| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 8763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8774 | 8774 |
| 8775 alternative_service_vector = | 8775 alternative_service_vector = |
| 8776 http_server_properties.GetAlternativeServices(http_host_port_pair); | 8776 http_server_properties.GetAlternativeServices(http_host_port_pair); |
| 8777 ASSERT_EQ(1u, alternative_service_vector.size()); | 8777 ASSERT_EQ(1u, alternative_service_vector.size()); |
| 8778 EXPECT_EQ(AlternateProtocolFromNextProto(GetParam()), | 8778 EXPECT_EQ(AlternateProtocolFromNextProto(GetParam()), |
| 8779 alternative_service_vector[0].protocol); | 8779 alternative_service_vector[0].protocol); |
| 8780 EXPECT_EQ("www.example.com", alternative_service_vector[0].host); | 8780 EXPECT_EQ("www.example.com", alternative_service_vector[0].host); |
| 8781 EXPECT_EQ(443, alternative_service_vector[0].port); | 8781 EXPECT_EQ(443, alternative_service_vector[0].port); |
| 8782 } | 8782 } |
| 8783 | 8783 |
| 8784 // Alternative Service headers must be ignored when |use_alternative_services| |
| 8785 // is false. |
| 8786 TEST_P(HttpNetworkTransactionTest, DoNotHonorAlternativeServiceHeader) { |
| 8787 session_deps_.next_protos = SpdyNextProtos(); |
| 8788 session_deps_.use_alternative_services = false; |
| 8789 |
| 8790 std::string alternative_service_http_header = |
| 8791 GetAlternativeServiceHttpHeader(); |
| 8792 |
| 8793 MockRead data_reads[] = { |
| 8794 MockRead("HTTP/1.1 200 OK\r\n"), |
| 8795 MockRead(alternative_service_http_header.c_str()), |
| 8796 MockRead("\r\n"), |
| 8797 MockRead("hello world"), |
| 8798 MockRead(SYNCHRONOUS, OK), |
| 8799 }; |
| 8800 |
| 8801 HttpRequestInfo request; |
| 8802 request.method = "GET"; |
| 8803 request.url = GURL("http://www.example.org/"); |
| 8804 request.load_flags = 0; |
| 8805 |
| 8806 StaticSocketDataProvider data(data_reads, arraysize(data_reads), nullptr, 0); |
| 8807 |
| 8808 session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 8809 |
| 8810 TestCompletionCallback callback; |
| 8811 |
| 8812 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 8813 scoped_ptr<HttpTransaction> trans( |
| 8814 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 8815 |
| 8816 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 8817 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8818 |
| 8819 HostPortPair http_host_port_pair("www.example.org", 80); |
| 8820 HttpServerProperties& http_server_properties = |
| 8821 *session->http_server_properties(); |
| 8822 AlternativeServiceVector alternative_service_vector = |
| 8823 http_server_properties.GetAlternativeServices(http_host_port_pair); |
| 8824 EXPECT_TRUE(alternative_service_vector.empty()); |
| 8825 |
| 8826 EXPECT_EQ(OK, callback.WaitForResult()); |
| 8827 |
| 8828 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 8829 ASSERT_TRUE(response != nullptr); |
| 8830 ASSERT_TRUE(response->headers.get() != nullptr); |
| 8831 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 8832 EXPECT_FALSE(response->was_fetched_via_spdy); |
| 8833 EXPECT_FALSE(response->was_npn_negotiated); |
| 8834 |
| 8835 std::string response_data; |
| 8836 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 8837 EXPECT_EQ("hello world", response_data); |
| 8838 |
| 8839 alternative_service_vector = |
| 8840 http_server_properties.GetAlternativeServices(http_host_port_pair); |
| 8841 EXPECT_TRUE(alternative_service_vector.empty()); |
| 8842 } |
| 8843 |
| 8784 TEST_P(HttpNetworkTransactionTest, HonorMultipleAlternativeServiceHeader) { | 8844 TEST_P(HttpNetworkTransactionTest, HonorMultipleAlternativeServiceHeader) { |
| 8785 session_deps_.next_protos = SpdyNextProtos(); | 8845 session_deps_.next_protos = SpdyNextProtos(); |
| 8786 session_deps_.use_alternative_services = true; | 8846 session_deps_.use_alternative_services = true; |
| 8787 | 8847 |
| 8788 MockRead data_reads[] = { | 8848 MockRead data_reads[] = { |
| 8789 MockRead("HTTP/1.1 200 OK\r\n"), | 8849 MockRead("HTTP/1.1 200 OK\r\n"), |
| 8790 MockRead("Alt-Svc: "), | 8850 MockRead("Alt-Svc: "), |
| 8791 MockRead(GetAlternateProtocolFromParam()), | 8851 MockRead(GetAlternateProtocolFromParam()), |
| 8792 MockRead("=\"www.example.com:443\";p=1.0,"), | 8852 MockRead("=\"www.example.com:443\";p=1.0,"), |
| 8793 MockRead("quic=\":1234\"\r\n\r\n"), | 8853 MockRead("quic=\":1234\"\r\n\r\n"), |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8838 ASSERT_EQ(2u, alternative_service_vector.size()); | 8898 ASSERT_EQ(2u, alternative_service_vector.size()); |
| 8839 EXPECT_EQ(AlternateProtocolFromNextProto(GetParam()), | 8899 EXPECT_EQ(AlternateProtocolFromNextProto(GetParam()), |
| 8840 alternative_service_vector[0].protocol); | 8900 alternative_service_vector[0].protocol); |
| 8841 EXPECT_EQ("www.example.com", alternative_service_vector[0].host); | 8901 EXPECT_EQ("www.example.com", alternative_service_vector[0].host); |
| 8842 EXPECT_EQ(443, alternative_service_vector[0].port); | 8902 EXPECT_EQ(443, alternative_service_vector[0].port); |
| 8843 EXPECT_EQ(QUIC, alternative_service_vector[1].protocol); | 8903 EXPECT_EQ(QUIC, alternative_service_vector[1].protocol); |
| 8844 EXPECT_EQ("www.example.org", alternative_service_vector[1].host); | 8904 EXPECT_EQ("www.example.org", alternative_service_vector[1].host); |
| 8845 EXPECT_EQ(1234, alternative_service_vector[1].port); | 8905 EXPECT_EQ(1234, alternative_service_vector[1].port); |
| 8846 } | 8906 } |
| 8847 | 8907 |
| 8908 // Alternate Protocol headers must be honored even if |use_alternative_services| |
| 8909 // is false. |
| 8848 TEST_P(HttpNetworkTransactionTest, HonorAlternateProtocolHeader) { | 8910 TEST_P(HttpNetworkTransactionTest, HonorAlternateProtocolHeader) { |
| 8849 session_deps_.next_protos = SpdyNextProtos(); | 8911 session_deps_.next_protos = SpdyNextProtos(); |
| 8850 session_deps_.use_alternative_services = true; | 8912 session_deps_.use_alternative_services = false; |
| 8851 | 8913 |
| 8852 std::string alternate_protocol_http_header = | 8914 std::string alternate_protocol_http_header = |
| 8853 GetAlternateProtocolHttpHeader(); | 8915 GetAlternateProtocolHttpHeader(); |
| 8854 | 8916 |
| 8855 MockRead data_reads[] = { | 8917 MockRead data_reads[] = { |
| 8856 MockRead("HTTP/1.1 200 OK\r\n"), | 8918 MockRead("HTTP/1.1 200 OK\r\n"), |
| 8857 MockRead(alternate_protocol_http_header.c_str()), | 8919 MockRead(alternate_protocol_http_header.c_str()), |
| 8858 MockRead("\r\n"), | 8920 MockRead("\r\n"), |
| 8859 MockRead("hello world"), | 8921 MockRead("hello world"), |
| 8860 MockRead(SYNCHRONOUS, OK), | 8922 MockRead(SYNCHRONOUS, OK), |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9024 | 9086 |
| 9025 alternative_service_vector = | 9087 alternative_service_vector = |
| 9026 http_server_properties.GetAlternativeServices(http_host_port_pair); | 9088 http_server_properties.GetAlternativeServices(http_host_port_pair); |
| 9027 ASSERT_EQ(1u, alternative_service_vector.size()); | 9089 ASSERT_EQ(1u, alternative_service_vector.size()); |
| 9028 EXPECT_EQ(AlternateProtocolFromNextProto(GetParam()), | 9090 EXPECT_EQ(AlternateProtocolFromNextProto(GetParam()), |
| 9029 alternative_service_vector[0].protocol); | 9091 alternative_service_vector[0].protocol); |
| 9030 EXPECT_EQ("www.example.com", alternative_service_vector[0].host); | 9092 EXPECT_EQ("www.example.com", alternative_service_vector[0].host); |
| 9031 EXPECT_EQ(443, alternative_service_vector[0].port); | 9093 EXPECT_EQ(443, alternative_service_vector[0].port); |
| 9032 } | 9094 } |
| 9033 | 9095 |
| 9096 // When |use_alternative_services| is false, do not observe alternative service |
| 9097 // entries that point to a different host. |
| 9098 TEST_P(HttpNetworkTransactionTest, DisableAlternativeServiceToDifferentHost) { |
| 9099 session_deps_.use_alternative_services = false; |
| 9100 |
| 9101 HttpRequestInfo request; |
| 9102 request.method = "GET"; |
| 9103 request.url = GURL("http://www.example.org/"); |
| 9104 request.load_flags = 0; |
| 9105 |
| 9106 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
| 9107 StaticSocketDataProvider first_data; |
| 9108 first_data.set_connect_data(mock_connect); |
| 9109 session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
| 9110 |
| 9111 MockRead data_reads[] = { |
| 9112 MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 9113 MockRead(ASYNC, OK), |
| 9114 }; |
| 9115 StaticSocketDataProvider second_data(data_reads, arraysize(data_reads), |
| 9116 nullptr, 0); |
| 9117 session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
| 9118 |
| 9119 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 9120 |
| 9121 base::WeakPtr<HttpServerProperties> http_server_properties = |
| 9122 session->http_server_properties(); |
| 9123 AlternativeService alternative_service( |
| 9124 AlternateProtocolFromNextProto(GetParam()), "different.example.org", 80); |
| 9125 base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 9126 http_server_properties->SetAlternativeService( |
| 9127 HostPortPair::FromURL(request.url), alternative_service, 1.0, expiration); |
| 9128 |
| 9129 scoped_ptr<HttpTransaction> trans( |
| 9130 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 9131 TestCompletionCallback callback; |
| 9132 |
| 9133 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 9134 // The connetion to origin was refused, and the alternative service should not |
| 9135 // be used (even though mock data are there), therefore the request should |
| 9136 // fail. |
| 9137 EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.GetResult(rv)); |
| 9138 } |
| 9139 |
| 9034 TEST_P(HttpNetworkTransactionTest, | 9140 TEST_P(HttpNetworkTransactionTest, |
| 9035 MarkBrokenAlternateProtocolAndFallback) { | 9141 MarkBrokenAlternateProtocolAndFallback) { |
| 9036 session_deps_.use_alternative_services = true; | 9142 session_deps_.use_alternative_services = true; |
| 9037 | 9143 |
| 9038 HttpRequestInfo request; | 9144 HttpRequestInfo request; |
| 9039 request.method = "GET"; | 9145 request.method = "GET"; |
| 9040 request.url = GURL("http://www.example.org/"); | 9146 request.url = GURL("http://www.example.org/"); |
| 9041 request.load_flags = 0; | 9147 request.load_flags = 0; |
| 9042 | 9148 |
| 9043 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); | 9149 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
| (...skipping 5342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 14386 ASSERT_TRUE(response); | 14492 ASSERT_TRUE(response); |
| 14387 ASSERT_TRUE(response->headers.get()); | 14493 ASSERT_TRUE(response->headers.get()); |
| 14388 | 14494 |
| 14389 EXPECT_EQ(101, response->headers->response_code()); | 14495 EXPECT_EQ(101, response->headers->response_code()); |
| 14390 | 14496 |
| 14391 trans.reset(); | 14497 trans.reset(); |
| 14392 session->CloseAllConnections(); | 14498 session->CloseAllConnections(); |
| 14393 } | 14499 } |
| 14394 | 14500 |
| 14395 } // namespace net | 14501 } // namespace net |
| OLD | NEW |