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 // A binary wrapper for QuicClient. | 5 // A binary wrapper for QuicClient. |
6 // Connects to a host using QUIC, sends a request to the provided URL, and | 6 // Connects to a host using QUIC, sends a request to the provided URL, and |
7 // displays the response. | 7 // displays the response. |
8 // | 8 // |
9 // Some usage examples: | 9 // Some usage examples: |
10 // | 10 // |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 #include "base/at_exit.h" | 43 #include "base/at_exit.h" |
44 #include "base/command_line.h" | 44 #include "base/command_line.h" |
45 #include "base/logging.h" | 45 #include "base/logging.h" |
46 #include "base/strings/string_number_conversions.h" | 46 #include "base/strings/string_number_conversions.h" |
47 #include "base/strings/string_split.h" | 47 #include "base/strings/string_split.h" |
48 #include "base/strings/string_util.h" | 48 #include "base/strings/string_util.h" |
49 #include "net/base/ip_endpoint.h" | 49 #include "net/base/ip_endpoint.h" |
50 #include "net/base/privacy_mode.h" | 50 #include "net/base/privacy_mode.h" |
51 #include "net/cert/cert_verifier.h" | 51 #include "net/cert/cert_verifier.h" |
52 #include "net/http/http_request_info.h" | |
53 #include "net/http/transport_security_state.h" | 52 #include "net/http/transport_security_state.h" |
54 #include "net/quic/crypto/proof_verifier_chromium.h" | 53 #include "net/quic/crypto/proof_verifier_chromium.h" |
55 #include "net/quic/quic_protocol.h" | 54 #include "net/quic/quic_protocol.h" |
56 #include "net/quic/quic_server_id.h" | 55 #include "net/quic/quic_server_id.h" |
57 #include "net/quic/quic_utils.h" | 56 #include "net/quic/quic_utils.h" |
58 #include "net/spdy/spdy_http_utils.h" | 57 #include "net/tools/epoll_server/epoll_server.h" |
59 #include "net/tools/quic/quic_simple_client.h" | 58 #include "net/tools/quic/quic_client.h" |
| 59 #include "net/tools/quic/spdy_utils.h" |
60 #include "url/gurl.h" | 60 #include "url/gurl.h" |
61 | 61 |
62 using base::StringPiece; | 62 using base::StringPiece; |
63 using net::CertVerifier; | 63 using net::CertVerifier; |
64 using net::ProofVerifierChromium; | 64 using net::ProofVerifierChromium; |
65 using net::TransportSecurityState; | 65 using net::TransportSecurityState; |
66 using std::cout; | 66 using std::cout; |
67 using std::cerr; | 67 using std::cerr; |
68 using std::map; | 68 using std::map; |
69 using std::string; | 69 using std::string; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 } | 154 } |
155 | 155 |
156 VLOG(1) << "server host: " << FLAGS_host << " port: " << FLAGS_port | 156 VLOG(1) << "server host: " << FLAGS_host << " port: " << FLAGS_port |
157 << " body: " << FLAGS_body << " headers: " << FLAGS_headers | 157 << " body: " << FLAGS_body << " headers: " << FLAGS_headers |
158 << " quiet: " << FLAGS_quiet | 158 << " quiet: " << FLAGS_quiet |
159 << " quic-version: " << FLAGS_quic_version | 159 << " quic-version: " << FLAGS_quic_version |
160 << " version_mismatch_ok: " << FLAGS_version_mismatch_ok | 160 << " version_mismatch_ok: " << FLAGS_version_mismatch_ok |
161 << " redirect_is_success: " << FLAGS_redirect_is_success; | 161 << " redirect_is_success: " << FLAGS_redirect_is_success; |
162 | 162 |
163 base::AtExitManager exit_manager; | 163 base::AtExitManager exit_manager; |
164 base::MessageLoopForIO message_loop; | |
165 | 164 |
166 // Determine IP address to connect to from supplied hostname. | 165 // Determine IP address to connect to from supplied hostname. |
167 net::IPAddressNumber ip_addr; | 166 net::IPAddressNumber ip_addr; |
168 | 167 |
169 // TODO(rtenneti): GURL's doesn't support default_protocol argument, thus | 168 // TODO(rtenneti): GURL's doesn't support default_protocol argument, thus |
170 // protocol is required in the URL. | 169 // protocol is required in the URL. |
171 GURL url(urls[0]); | 170 GURL url(urls[0]); |
172 string host = FLAGS_host; | 171 string host = FLAGS_host; |
173 // TODO(rtenneti): get ip_addr from hostname by doing host resolution. | 172 // TODO(rtenneti): get ip_addr from hostname by doing host resolution. |
174 if (host.empty()) { | 173 if (host.empty()) { |
175 LOG(ERROR) << "--host must be specified\n"; | 174 LOG(ERROR) << "--host must be specified\n"; |
176 return 1; | 175 return 1; |
177 } | 176 } |
178 if (!net::ParseIPLiteralToNumber(host, &ip_addr)) { | 177 if (!net::ParseIPLiteralToNumber(host, &ip_addr)) { |
179 LOG(ERROR) << "--host could not be parsed as an IP address\n"; | 178 LOG(ERROR) << "--host could not be parsed as an IP address\n"; |
180 return 1; | 179 return 1; |
181 } | 180 } |
182 | 181 |
183 string host_port = net::IPAddressToStringWithPort(ip_addr, FLAGS_port); | 182 string host_port = net::IPAddressToStringWithPort(ip_addr, FLAGS_port); |
184 VLOG(1) << "Resolved " << host << " to " << host_port << endl; | 183 VLOG(1) << "Resolved " << host << " to " << host_port << endl; |
185 | 184 |
186 // Build the client, and try to connect. | 185 // Build the client, and try to connect. |
187 bool is_https = (FLAGS_port == 443); | 186 bool is_https = (FLAGS_port == 443); |
| 187 net::EpollServer epoll_server; |
188 net::QuicServerId server_id(host, FLAGS_port, is_https, | 188 net::QuicServerId server_id(host, FLAGS_port, is_https, |
189 net::PRIVACY_MODE_DISABLED); | 189 net::PRIVACY_MODE_DISABLED); |
190 net::QuicVersionVector versions = net::QuicSupportedVersions(); | 190 net::QuicVersionVector versions = net::QuicSupportedVersions(); |
191 if (FLAGS_quic_version != -1) { | 191 if (FLAGS_quic_version != -1) { |
192 versions.clear(); | 192 versions.clear(); |
193 versions.push_back(static_cast<net::QuicVersion>(FLAGS_quic_version)); | 193 versions.push_back(static_cast<net::QuicVersion>(FLAGS_quic_version)); |
194 } | 194 } |
195 net::tools::QuicSimpleClient client(net::IPEndPoint(ip_addr, FLAGS_port), | 195 net::tools::QuicClient client(net::IPEndPoint(ip_addr, FLAGS_port), server_id, |
196 server_id, versions); | 196 versions, &epoll_server); |
197 scoped_ptr<CertVerifier> cert_verifier; | 197 scoped_ptr<CertVerifier> cert_verifier; |
198 scoped_ptr<TransportSecurityState> transport_security_state; | 198 scoped_ptr<TransportSecurityState> transport_security_state; |
199 if (is_https) { | 199 if (is_https) { |
200 // For secure QUIC we need to verify the cert chain.a | 200 // For secure QUIC we need to verify the cert chain.a |
201 cert_verifier.reset(CertVerifier::CreateDefault()); | 201 cert_verifier.reset(CertVerifier::CreateDefault()); |
202 transport_security_state.reset(new TransportSecurityState); | 202 transport_security_state.reset(new TransportSecurityState); |
203 // TODO(rtenneti): Fix "Proof invalid: Missing context" error. | 203 // TODO(rtenneti): Fix "Proof invalid: Missing context" error. |
204 client.SetProofVerifier(new ProofVerifierChromium( | 204 client.SetProofVerifier(new ProofVerifierChromium( |
205 cert_verifier.get(), transport_security_state.get())); | 205 cert_verifier.get(), transport_security_state.get())); |
206 } | 206 } |
207 if (!client.Initialize()) { | 207 if (!client.Initialize()) { |
208 cerr << "Failed to initialize client." << endl; | 208 cerr << "Failed to initialize client." << endl; |
209 return 1; | 209 return 1; |
210 } | 210 } |
211 if (!client.Connect()) { | 211 if (!client.Connect()) { |
212 net::QuicErrorCode error = client.session()->error(); | 212 net::QuicErrorCode error = client.session()->error(); |
213 if (FLAGS_version_mismatch_ok && error == net::QUIC_INVALID_VERSION) { | 213 if (FLAGS_version_mismatch_ok && error == net::QUIC_INVALID_VERSION) { |
214 cout << "Server talks QUIC, but none of the versions supoorted by " | 214 cout << "Server talks QUIC, but none of the versions supoorted by " |
215 << "this client: " << QuicVersionVectorToString(versions) << endl; | 215 << "this client: " << QuicVersionVectorToString(versions) << endl; |
216 // Version mismatch is not deemed a failure. | 216 // Version mismatch is not deemed a failure. |
217 return 0; | 217 return 0; |
218 } | 218 } |
219 cerr << "Failed to connect to " << host_port | 219 cerr << "Failed to connect to " << host_port |
220 << ". Error: " << net::QuicUtils::ErrorToString(error) << endl; | 220 << ". Error: " << net::QuicUtils::ErrorToString(error) << endl; |
221 return 1; | 221 return 1; |
222 } | 222 } |
223 cout << "Connected to " << host_port << endl; | 223 cout << "Connected to " << host_port << endl; |
224 | 224 |
225 // Construct a GET or POST request for supplied URL. | 225 // Construct a GET or POST request for supplied URL. |
226 net::HttpRequestInfo request; | 226 net::BalsaHeaders headers; |
227 request.method = FLAGS_body.empty() ? "GET" : "POST"; | 227 headers.SetRequestFirstlineFromStringPieces( |
228 request.url = url; | 228 FLAGS_body.empty() ? "GET" : "POST", url.spec(), "HTTP/1.1"); |
229 | 229 |
230 // Append any additional headers supplied on the command line. | 230 // Append any additional headers supplied on the command line. |
231 vector<string> headers_tokenized; | 231 vector<string> headers_tokenized; |
232 Tokenize(FLAGS_headers, ";", &headers_tokenized); | 232 Tokenize(FLAGS_headers, ";", &headers_tokenized); |
233 for (size_t i = 0; i < headers_tokenized.size(); ++i) { | 233 for (size_t i = 0; i < headers_tokenized.size(); ++i) { |
234 string sp; | 234 string sp; |
235 base::TrimWhitespaceASCII(headers_tokenized[i], base::TRIM_ALL, &sp); | 235 base::TrimWhitespaceASCII(headers_tokenized[i], base::TRIM_ALL, &sp); |
236 if (sp.empty()) { | 236 if (sp.empty()) { |
237 continue; | 237 continue; |
238 } | 238 } |
239 vector<string> kv; | 239 vector<string> kv; |
240 base::SplitString(sp, ':', &kv); | 240 base::SplitString(sp, ':', &kv); |
241 CHECK_EQ(2u, kv.size()); | 241 CHECK_EQ(2u, kv.size()); |
242 string key; | 242 string key; |
243 base::TrimWhitespaceASCII(kv[0], base::TRIM_ALL, &key); | 243 base::TrimWhitespaceASCII(kv[0], base::TRIM_ALL, &key); |
244 string value; | 244 string value; |
245 base::TrimWhitespaceASCII(kv[1], base::TRIM_ALL, &value); | 245 base::TrimWhitespaceASCII(kv[1], base::TRIM_ALL, &value); |
246 request.extra_headers.SetHeader(key, value); | 246 headers.AppendHeader(key, value); |
247 } | 247 } |
248 | 248 |
249 // Make sure to store the response, for later output. | 249 // Make sure to store the response, for later output. |
250 client.set_store_response(true); | 250 client.set_store_response(true); |
251 | 251 |
252 // Send the request. | 252 // Send the request. |
253 net::SpdyHeaderBlock header_block; | 253 map<string, string> header_block = |
254 net::CreateSpdyHeadersFromHttpRequest(request, request.extra_headers, | 254 net::tools::SpdyUtils::RequestHeadersToSpdy4Headers(headers); |
255 net::SPDY3, /*direct=*/ true, | 255 client.SendRequestAndWaitForResponse(headers, FLAGS_body, /*fin=*/true); |
256 &header_block); | |
257 client.SendRequestAndWaitForResponse(request, FLAGS_body, /*fin=*/true); | |
258 | 256 |
259 // Print request and response details. | 257 // Print request and response details. |
260 if (!FLAGS_quiet) { | 258 if (!FLAGS_quiet) { |
261 cout << "Request:" << endl; | 259 cout << "Request:" << endl; |
262 cout << "headers:" << endl; | 260 cout << "headers:" << endl; |
263 for (const std::pair<string, string>& kv : header_block) { | 261 for (const std::pair<string, string>& kv : header_block) { |
264 cout << " " << kv.first << ": " << kv.second << endl; | 262 cout << " " << kv.first << ": " << kv.second << endl; |
265 } | 263 } |
266 cout << "body: " << FLAGS_body << endl; | 264 cout << "body: " << FLAGS_body << endl; |
267 cout << endl; | 265 cout << endl << "Response:"; |
268 cout << "Response:" << endl; | |
269 cout << "headers: " << client.latest_response_headers() << endl; | 266 cout << "headers: " << client.latest_response_headers() << endl; |
270 cout << "body: " << client.latest_response_body() << endl; | 267 cout << "body: " << client.latest_response_body() << endl; |
271 } | 268 } |
272 | 269 |
273 size_t response_code = client.latest_response_code(); | 270 size_t response_code = client.latest_response_code(); |
274 if (response_code >= 200 && response_code < 300) { | 271 if (response_code >= 200 && response_code < 300) { |
275 cout << "Request succeeded (" << response_code << ")." << endl; | 272 cout << "Request succeeded (" << response_code << ")." << endl; |
276 return 0; | 273 return 0; |
277 } else if (response_code >= 300 && response_code < 400) { | 274 } else if (response_code >= 300 && response_code < 400) { |
278 if (FLAGS_redirect_is_success) { | 275 if (FLAGS_redirect_is_success) { |
279 cout << "Request succeeded (redirect " << response_code << ")." << endl; | 276 cout << "Request succeeded (redirect " << response_code << ")." << endl; |
280 return 0; | 277 return 0; |
281 } else { | 278 } else { |
282 cout << "Request failed (redirect " << response_code << ")." << endl; | 279 cout << "Request failed (redirect " << response_code << ")." << endl; |
283 return 1; | 280 return 1; |
284 } | 281 } |
285 } else { | 282 } else { |
286 cerr << "Request failed (" << response_code << ")." << endl; | 283 cerr << "Request failed (" << response_code << ")." << endl; |
287 return 1; | 284 return 1; |
288 } | 285 } |
289 } | 286 } |
OLD | NEW |