| 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/tools/quic/test_tools/quic_test_client.h" | 5 #include "net/tools/quic/test_tools/quic_test_client.h" |
| 6 | 6 |
| 7 #include "googleurl/src/gurl.h" | 7 #include "googleurl/src/gurl.h" |
| 8 #include "net/tools/flip_server/balsa_headers.h" | 8 #include "net/tools/flip_server/balsa_headers.h" |
| 9 #include "net/tools/quic/test_tools/http_message_test_utils.h" | 9 #include "net/tools/quic/test_tools/http_message_test_utils.h" |
| 10 | 10 |
| 11 using std::string; | 11 using std::string; |
| 12 using base::StringPiece; | 12 using base::StringPiece; |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 namespace tools { | 15 namespace tools { |
| 16 namespace test { | 16 namespace test { |
| 17 | 17 |
| 18 BalsaHeaders* MungeHeaders(const BalsaHeaders* const_headers) { | 18 BalsaHeaders* MungeHeaders(const BalsaHeaders* const_headers, |
| 19 bool secure) { |
| 19 StringPiece uri = const_headers->request_uri(); | 20 StringPiece uri = const_headers->request_uri(); |
| 20 if (uri.empty()) { | 21 if (uri.empty()) { |
| 21 return NULL; | 22 return NULL; |
| 22 } | 23 } |
| 23 if (const_headers->request_method() == "CONNECT") { | 24 if (const_headers->request_method() == "CONNECT") { |
| 24 return NULL; | 25 return NULL; |
| 25 } | 26 } |
| 26 BalsaHeaders* headers = new BalsaHeaders; | 27 BalsaHeaders* headers = new BalsaHeaders; |
| 27 headers->CopyFrom(*const_headers); | 28 headers->CopyFrom(*const_headers); |
| 28 if (!uri.starts_with("https://") && | 29 if (!uri.starts_with("https://") && |
| 29 !uri.starts_with("http://")) { | 30 !uri.starts_with("http://")) { |
| 30 // If we have a relative URL, set some defaults. | 31 // If we have a relative URL, set some defaults. |
| 31 string full_uri = "https:/www.google.com"; | 32 string full_uri = secure ? "https://www.google.com" : |
| 33 "http://www.google.com"; |
| 32 full_uri.append(uri.as_string()); | 34 full_uri.append(uri.as_string()); |
| 33 headers->SetRequestUri(full_uri); | 35 headers->SetRequestUri(full_uri); |
| 34 } | 36 } |
| 35 return headers; | 37 return headers; |
| 36 } | 38 } |
| 37 | 39 |
| 38 QuicTestClient::QuicTestClient(IPEndPoint address, const string& hostname) | 40 QuicTestClient::QuicTestClient(IPEndPoint address, const string& hostname) |
| 39 : server_address_(address), | 41 : client_(address, hostname) { |
| 40 client_(address, hostname), | 42 Initialize(address, hostname); |
| 41 stream_(NULL), | 43 } |
| 42 stream_error_(QUIC_STREAM_NO_ERROR), | 44 |
| 43 connection_error_(QUIC_NO_ERROR), | 45 QuicTestClient::QuicTestClient(IPEndPoint address, |
| 44 bytes_read_(0), | 46 const string& hostname, |
| 45 bytes_written_(0), | 47 bool secure) |
| 46 never_connected_(true) { | 48 : client_(address, hostname) { |
| 49 Initialize(address, hostname); |
| 50 secure_ = secure; |
| 47 } | 51 } |
| 48 | 52 |
| 49 QuicTestClient::QuicTestClient(IPEndPoint address, | 53 QuicTestClient::QuicTestClient(IPEndPoint address, |
| 50 const string& hostname, | 54 const string& hostname, |
| 51 const QuicConfig& config) | 55 const QuicConfig& config) |
| 52 : server_address_(address), | 56 : client_(address, hostname, config) { |
| 53 client_(address, hostname, config), | 57 Initialize(address, hostname); |
| 54 stream_(NULL), | 58 } |
| 55 stream_error_(QUIC_STREAM_NO_ERROR), | 59 |
| 56 connection_error_(QUIC_NO_ERROR), | 60 void QuicTestClient::Initialize(IPEndPoint address, const string& hostname) { |
| 57 bytes_read_(0), | 61 server_address_ = address; |
| 58 bytes_written_(0), | 62 stream_ = NULL; |
| 59 never_connected_(true) { | 63 stream_error_ = QUIC_STREAM_NO_ERROR; |
| 64 connection_error_ = QUIC_NO_ERROR; |
| 65 bytes_read_ = 0; |
| 66 bytes_written_= 0; |
| 67 never_connected_ =true; |
| 68 secure_ = true; |
| 69 auto_reconnect_ = false; |
| 60 } | 70 } |
| 61 | 71 |
| 62 QuicTestClient::~QuicTestClient() { | 72 QuicTestClient::~QuicTestClient() { |
| 63 if (stream_) { | 73 if (stream_) { |
| 64 stream_->set_visitor(NULL); | 74 stream_->set_visitor(NULL); |
| 65 } | 75 } |
| 66 } | 76 } |
| 67 | 77 |
| 68 ssize_t QuicTestClient::SendRequest(const string& uri) { | 78 ssize_t QuicTestClient::SendRequest(const string& uri) { |
| 69 HTTPMessage message(HttpConstants::HTTP_1_1, HttpConstants::GET, uri); | 79 HTTPMessage message(HttpConstants::HTTP_1_1, HttpConstants::GET, uri); |
| 70 return SendMessage(message); | 80 return SendMessage(message); |
| 71 } | 81 } |
| 72 | 82 |
| 73 ssize_t QuicTestClient::SendMessage(const HTTPMessage& message) { | 83 ssize_t QuicTestClient::SendMessage(const HTTPMessage& message) { |
| 74 stream_ = NULL; // Always force creation of a stream for SendMessage. | 84 stream_ = NULL; // Always force creation of a stream for SendMessage. |
| 75 | 85 |
| 76 // If we're not connected, try to find an sni hostname. | 86 // If we're not connected, try to find an sni hostname. |
| 77 if (!connected()) { | 87 if (!connected()) { |
| 78 GURL url(message.headers()->request_uri().as_string()); | 88 GURL url(message.headers()->request_uri().as_string()); |
| 79 if (!url.host().empty()) { | 89 if (!url.host().empty()) { |
| 80 client_.set_server_hostname(url.host()); | 90 client_.set_server_hostname(url.host()); |
| 81 } | 91 } |
| 82 } | 92 } |
| 83 | 93 |
| 84 QuicReliableClientStream* stream = GetOrCreateStream(); | 94 QuicReliableClientStream* stream = GetOrCreateStream(); |
| 85 if (!stream) { return 0; } | 95 if (!stream) { return 0; } |
| 86 | 96 |
| 87 scoped_ptr<BalsaHeaders> munged_headers(MungeHeaders(message.headers())); | 97 scoped_ptr<BalsaHeaders> munged_headers(MungeHeaders(message.headers(), |
| 98 secure_)); |
| 88 return GetOrCreateStream()->SendRequest( | 99 return GetOrCreateStream()->SendRequest( |
| 89 munged_headers.get() ? *munged_headers.get() : *message.headers(), | 100 munged_headers.get() ? *munged_headers.get() : *message.headers(), |
| 90 message.body(), | 101 message.body(), |
| 91 message.has_complete_message()); | 102 message.has_complete_message()); |
| 92 } | 103 } |
| 93 | 104 |
| 94 ssize_t QuicTestClient::SendData(string data, bool last_data) { | 105 ssize_t QuicTestClient::SendData(string data, bool last_data) { |
| 95 QuicReliableClientStream* stream = GetOrCreateStream(); | 106 QuicReliableClientStream* stream = GetOrCreateStream(); |
| 96 if (!stream) { return 0; } | 107 if (!stream) { return 0; } |
| 97 GetOrCreateStream()->SendBody(data, last_data); | 108 GetOrCreateStream()->SendBody(data, last_data); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 108 string QuicTestClient::SendSynchronousRequest(const string& uri) { | 119 string QuicTestClient::SendSynchronousRequest(const string& uri) { |
| 109 if (SendRequest(uri) == 0) { | 120 if (SendRequest(uri) == 0) { |
| 110 DLOG(ERROR) << "Failed the request for uri:" << uri; | 121 DLOG(ERROR) << "Failed the request for uri:" << uri; |
| 111 return ""; | 122 return ""; |
| 112 } | 123 } |
| 113 WaitForResponse(); | 124 WaitForResponse(); |
| 114 return response_; | 125 return response_; |
| 115 } | 126 } |
| 116 | 127 |
| 117 QuicReliableClientStream* QuicTestClient::GetOrCreateStream() { | 128 QuicReliableClientStream* QuicTestClient::GetOrCreateStream() { |
| 118 if (never_connected_ == true) { | 129 if (never_connected_ == true || auto_reconnect_) { |
| 119 if (!connected()) { | 130 if (!connected()) { |
| 120 Connect(); | 131 Connect(); |
| 121 } | 132 } |
| 122 if (!connected()) { | 133 if (!connected()) { |
| 123 return NULL; | 134 return NULL; |
| 124 } | 135 } |
| 125 } | 136 } |
| 126 if (!stream_) { | 137 if (!stream_) { |
| 127 stream_ = client_.CreateReliableClientStream(); | 138 stream_ = client_.CreateReliableClientStream(); |
| 128 stream_->set_visitor(this); | 139 if (stream_ != NULL) { |
| 140 stream_->set_visitor(this); |
| 141 } |
| 129 } | 142 } |
| 130 return stream_; | 143 return stream_; |
| 131 } | 144 } |
| 132 | 145 |
| 133 bool QuicTestClient::connected() const { | 146 bool QuicTestClient::connected() const { |
| 134 return client_.connected(); | 147 return client_.connected(); |
| 135 } | 148 } |
| 136 | 149 |
| 137 void QuicTestClient::WaitForResponse() { | 150 void QuicTestClient::WaitForResponse() { |
| 151 if (stream_ == NULL) { |
| 152 // The client has likely disconnected. |
| 153 return; |
| 154 } |
| 138 client_.WaitForStreamToClose(stream_->id()); | 155 client_.WaitForStreamToClose(stream_->id()); |
| 139 } | 156 } |
| 140 | 157 |
| 141 void QuicTestClient::Connect() { | 158 void QuicTestClient::Connect() { |
| 142 DCHECK(!connected()); | 159 DCHECK(!connected()); |
| 143 client_.Initialize(); | 160 client_.Initialize(); |
| 144 client_.Connect(); | 161 client_.Connect(); |
| 145 never_connected_ = false; | 162 never_connected_ = false; |
| 146 } | 163 } |
| 147 | 164 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 stream_error_ = stream_->stream_error(); | 217 stream_error_ = stream_->stream_error(); |
| 201 connection_error_ = stream_->connection_error(); | 218 connection_error_ = stream_->connection_error(); |
| 202 bytes_read_ = stream_->stream_bytes_read(); | 219 bytes_read_ = stream_->stream_bytes_read(); |
| 203 bytes_written_ = stream_->stream_bytes_written(); | 220 bytes_written_ = stream_->stream_bytes_written(); |
| 204 stream_ = NULL; | 221 stream_ = NULL; |
| 205 } | 222 } |
| 206 | 223 |
| 207 } // namespace test | 224 } // namespace test |
| 208 } // namespace tools | 225 } // namespace tools |
| 209 } // namespace net | 226 } // namespace net |
| OLD | NEW |