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 26 matching lines...) Expand all Loading... |
37 // Get IP address of the www.example.com | 37 // Get IP address of the www.example.com |
38 // IP=`dig www.example.com +short | head -1` | 38 // IP=`dig www.example.com +short | head -1` |
39 // quic_client http://www.example.com --host=${IP} | 39 // quic_client http://www.example.com --host=${IP} |
40 | 40 |
41 #include <iostream> | 41 #include <iostream> |
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/message_loop/message_loop.h" | 46 #include "base/message_loop/message_loop.h" |
47 #include "base/strings/string_number_conversions.h" | |
48 #include "base/strings/string_split.h" | |
49 #include "base/strings/string_util.h" | |
50 #include "net/base/ip_address.h" | |
51 #include "net/base/ip_endpoint.h" | |
52 #include "net/base/net_errors.h" | 47 #include "net/base/net_errors.h" |
53 #include "net/base/privacy_mode.h" | 48 #include "net/base/privacy_mode.h" |
54 #include "net/cert/cert_verifier.h" | 49 #include "net/cert/cert_verifier.h" |
55 #include "net/cert/multi_log_ct_verifier.h" | 50 #include "net/cert/multi_log_ct_verifier.h" |
56 #include "net/http/http_request_info.h" | |
57 #include "net/http/transport_security_state.h" | 51 #include "net/http/transport_security_state.h" |
58 #include "net/quic/chromium/crypto/proof_verifier_chromium.h" | 52 #include "net/quic/chromium/crypto/proof_verifier_chromium.h" |
59 #include "net/quic/core/quic_error_codes.h" | 53 #include "net/quic/core/quic_error_codes.h" |
60 #include "net/quic/core/quic_packets.h" | 54 #include "net/quic/core/quic_packets.h" |
61 #include "net/quic/core/quic_server_id.h" | 55 #include "net/quic/core/quic_server_id.h" |
| 56 #include "net/quic/platform/api/quic_socket_address.h" |
| 57 #include "net/quic/platform/api/quic_str_cat.h" |
62 #include "net/quic/platform/api/quic_text_utils.h" | 58 #include "net/quic/platform/api/quic_text_utils.h" |
63 #include "net/spdy/spdy_header_block.h" | 59 #include "net/spdy/spdy_header_block.h" |
64 #include "net/spdy/spdy_http_utils.h" | 60 #include "net/spdy/spdy_http_utils.h" |
65 #include "net/tools/quic/quic_simple_client.h" | 61 #include "net/tools/quic/quic_simple_client.h" |
66 #include "net/tools/quic/synchronous_host_resolver.h" | 62 #include "net/tools/quic/synchronous_host_resolver.h" |
67 #include "url/gurl.h" | 63 #include "url/gurl.h" |
68 | 64 |
69 using base::StringPiece; | 65 using base::StringPiece; |
70 using net::CertVerifier; | 66 using net::CertVerifier; |
71 using net::CTPolicyEnforcer; | 67 using net::CTPolicyEnforcer; |
72 using net::CTVerifier; | 68 using net::CTVerifier; |
73 using net::MultiLogCTVerifier; | 69 using net::MultiLogCTVerifier; |
74 using net::ProofVerifier; | 70 using net::ProofVerifier; |
75 using net::ProofVerifierChromium; | 71 using net::ProofVerifierChromium; |
76 using net::QuicTextUtils; | 72 using net::QuicTextUtils; |
| 73 using net::SpdyHeaderBlock; |
77 using net::TransportSecurityState; | 74 using net::TransportSecurityState; |
78 using std::cout; | 75 using std::cout; |
79 using std::cerr; | 76 using std::cerr; |
80 using std::endl; | 77 using std::endl; |
81 using std::string; | 78 using std::string; |
82 | 79 |
83 // The IP or hostname the quic client will connect to. | 80 // The IP or hostname the quic client will connect to. |
84 string FLAGS_host = ""; | 81 string FLAGS_host = ""; |
85 // The port to connect to. | 82 // The port to connect to. |
86 int32_t FLAGS_port = 0; | 83 int32_t FLAGS_port = 0; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 << " quiet: " << FLAGS_quiet | 213 << " quiet: " << FLAGS_quiet |
217 << " quic-version: " << FLAGS_quic_version | 214 << " quic-version: " << FLAGS_quic_version |
218 << " version_mismatch_ok: " << FLAGS_version_mismatch_ok | 215 << " version_mismatch_ok: " << FLAGS_version_mismatch_ok |
219 << " redirect_is_success: " << FLAGS_redirect_is_success | 216 << " redirect_is_success: " << FLAGS_redirect_is_success |
220 << " initial_mtu: " << FLAGS_initial_mtu; | 217 << " initial_mtu: " << FLAGS_initial_mtu; |
221 | 218 |
222 base::AtExitManager exit_manager; | 219 base::AtExitManager exit_manager; |
223 base::MessageLoopForIO message_loop; | 220 base::MessageLoopForIO message_loop; |
224 | 221 |
225 // Determine IP address to connect to from supplied hostname. | 222 // Determine IP address to connect to from supplied hostname. |
226 net::IPAddress ip_addr; | 223 net::QuicIpAddress ip_addr; |
227 | 224 |
228 // TODO(rtenneti): GURL's doesn't support default_protocol argument, thus | |
229 // protocol is required in the URL. | |
230 GURL url(urls[0]); | 225 GURL url(urls[0]); |
231 string host = FLAGS_host; | 226 string host = FLAGS_host; |
232 if (host.empty()) { | 227 if (host.empty()) { |
233 host = url.host(); | 228 host = url.host(); |
234 } | 229 } |
235 int port = FLAGS_port; | 230 int port = FLAGS_port; |
236 if (port == 0) { | 231 if (port == 0) { |
237 port = url.EffectiveIntPort(); | 232 port = url.EffectiveIntPort(); |
238 } | 233 } |
239 if (!ip_addr.AssignFromIPLiteral(host)) { | 234 if (!ip_addr.FromString(host)) { |
240 net::AddressList addresses; | 235 net::AddressList addresses; |
241 int rv = net::SynchronousHostResolver::Resolve(host, &addresses); | 236 int rv = net::SynchronousHostResolver::Resolve(host, &addresses); |
242 if (rv != net::OK) { | 237 if (rv != net::OK) { |
243 LOG(ERROR) << "Unable to resolve '" << host | 238 LOG(ERROR) << "Unable to resolve '" << host |
244 << "' : " << net::ErrorToShortString(rv); | 239 << "' : " << net::ErrorToShortString(rv); |
245 return 1; | 240 return 1; |
246 } | 241 } |
247 ip_addr = addresses[0].address(); | 242 ip_addr = |
| 243 net::QuicIpAddress(net::QuicIpAddressImpl(addresses[0].address())); |
248 } | 244 } |
249 | 245 |
250 string host_port = net::IPAddressToStringWithPort(ip_addr, FLAGS_port); | 246 string host_port = net::QuicStrCat(ip_addr.ToString(), ":", port); |
251 VLOG(1) << "Resolved " << host << " to " << host_port << endl; | 247 VLOG(1) << "Resolved " << host << " to " << host_port << endl; |
252 | 248 |
253 // Build the client, and try to connect. | 249 // Build the client, and try to connect. |
254 net::QuicServerId server_id(url.host(), url.EffectiveIntPort(), | 250 net::QuicServerId server_id(url.host(), url.EffectiveIntPort(), |
255 net::PRIVACY_MODE_DISABLED); | 251 net::PRIVACY_MODE_DISABLED); |
256 net::QuicVersionVector versions = net::AllSupportedVersions(); | 252 net::QuicVersionVector versions = net::AllSupportedVersions(); |
257 if (FLAGS_quic_version != -1) { | 253 if (FLAGS_quic_version != -1) { |
258 versions.clear(); | 254 versions.clear(); |
259 versions.push_back(static_cast<net::QuicVersion>(FLAGS_quic_version)); | 255 versions.push_back(static_cast<net::QuicVersion>(FLAGS_quic_version)); |
260 } | 256 } |
261 // For secure QUIC we need to verify the cert chain. | 257 // For secure QUIC we need to verify the cert chain. |
262 std::unique_ptr<CertVerifier> cert_verifier(CertVerifier::CreateDefault()); | 258 std::unique_ptr<CertVerifier> cert_verifier(CertVerifier::CreateDefault()); |
263 std::unique_ptr<TransportSecurityState> transport_security_state( | 259 std::unique_ptr<TransportSecurityState> transport_security_state( |
264 new TransportSecurityState); | 260 new TransportSecurityState); |
265 std::unique_ptr<CTVerifier> ct_verifier(new MultiLogCTVerifier()); | 261 std::unique_ptr<CTVerifier> ct_verifier(new MultiLogCTVerifier()); |
266 std::unique_ptr<CTPolicyEnforcer> ct_policy_enforcer(new CTPolicyEnforcer()); | 262 std::unique_ptr<CTPolicyEnforcer> ct_policy_enforcer(new CTPolicyEnforcer()); |
267 std::unique_ptr<ProofVerifier> proof_verifier; | 263 std::unique_ptr<ProofVerifier> proof_verifier; |
268 if (line->HasSwitch("disable-certificate-verification")) { | 264 if (line->HasSwitch("disable-certificate-verification")) { |
269 proof_verifier.reset(new FakeProofVerifier()); | 265 proof_verifier.reset(new FakeProofVerifier()); |
270 } else { | 266 } else { |
271 proof_verifier.reset(new ProofVerifierChromium( | 267 proof_verifier.reset(new ProofVerifierChromium( |
272 cert_verifier.get(), ct_policy_enforcer.get(), | 268 cert_verifier.get(), ct_policy_enforcer.get(), |
273 transport_security_state.get(), ct_verifier.get())); | 269 transport_security_state.get(), ct_verifier.get())); |
274 } | 270 } |
275 net::QuicSimpleClient client(net::IPEndPoint(ip_addr, port), server_id, | 271 net::QuicSimpleClient client(net::QuicSocketAddress(ip_addr, port), server_id, |
276 versions, std::move(proof_verifier)); | 272 versions, std::move(proof_verifier)); |
277 client.set_initial_max_packet_length( | 273 client.set_initial_max_packet_length( |
278 FLAGS_initial_mtu != 0 ? FLAGS_initial_mtu : net::kDefaultMaxPacketSize); | 274 FLAGS_initial_mtu != 0 ? FLAGS_initial_mtu : net::kDefaultMaxPacketSize); |
279 if (!client.Initialize()) { | 275 if (!client.Initialize()) { |
280 cerr << "Failed to initialize client." << endl; | 276 cerr << "Failed to initialize client." << endl; |
281 return 1; | 277 return 1; |
282 } | 278 } |
283 if (!client.Connect()) { | 279 if (!client.Connect()) { |
284 net::QuicErrorCode error = client.session()->error(); | 280 net::QuicErrorCode error = client.session()->error(); |
285 if (FLAGS_version_mismatch_ok && error == net::QUIC_INVALID_VERSION) { | 281 if (FLAGS_version_mismatch_ok && error == net::QUIC_INVALID_VERSION) { |
286 cout << "Server talks QUIC, but none of the versions supported by " | 282 cout << "Server talks QUIC, but none of the versions supported by " |
287 << "this client: " << QuicVersionVectorToString(versions) << endl; | 283 << "this client: " << QuicVersionVectorToString(versions) << endl; |
288 // Version mismatch is not deemed a failure. | 284 // Version mismatch is not deemed a failure. |
289 return 0; | 285 return 0; |
290 } | 286 } |
291 cerr << "Failed to connect to " << host_port | 287 cerr << "Failed to connect to " << host_port |
292 << ". Error: " << net::QuicErrorCodeToString(error) << endl; | 288 << ". Error: " << net::QuicErrorCodeToString(error) << endl; |
293 return 1; | 289 return 1; |
294 } | 290 } |
295 cout << "Connected to " << host_port << endl; | 291 cout << "Connected to " << host_port << endl; |
296 | 292 |
297 // Construct the string body from flags, if provided. | 293 // Construct the string body from flags, if provided. |
298 string body = FLAGS_body; | 294 string body = FLAGS_body; |
299 if (!FLAGS_body_hex.empty()) { | 295 if (!FLAGS_body_hex.empty()) { |
300 DCHECK(FLAGS_body.empty()) << "Only set one of --body and --body_hex."; | 296 DCHECK(FLAGS_body.empty()) << "Only set one of --body and --body_hex."; |
301 body = QuicTextUtils::HexDecode(FLAGS_body_hex); | 297 body = QuicTextUtils::HexDecode(FLAGS_body_hex); |
302 } | 298 } |
303 | 299 |
304 // Construct a GET or POST request for supplied URL. | 300 // Construct a GET or POST request for supplied URL. |
305 net::HttpRequestInfo request; | 301 SpdyHeaderBlock header_block; |
306 request.method = body.empty() ? "GET" : "POST"; | 302 header_block[":method"] = body.empty() ? "GET" : "POST"; |
307 request.url = url; | 303 header_block[":scheme"] = url.scheme(); |
| 304 header_block[":authority"] = url.host(); |
| 305 header_block[":path"] = url.path(); |
308 | 306 |
309 // Append any additional headers supplied on the command line. | 307 // Append any additional headers supplied on the command line. |
310 for (const std::string& header : | 308 for (StringPiece sp : QuicTextUtils::Split(FLAGS_headers, ';')) { |
311 base::SplitString(FLAGS_headers, ";", base::KEEP_WHITESPACE, | 309 QuicTextUtils::RemoveLeadingAndTrailingWhitespace(&sp); |
312 base::SPLIT_WANT_NONEMPTY)) { | |
313 string sp; | |
314 base::TrimWhitespaceASCII(header, base::TRIM_ALL, &sp); | |
315 if (sp.empty()) { | 310 if (sp.empty()) { |
316 continue; | 311 continue; |
317 } | 312 } |
318 std::vector<string> kv = | 313 std::vector<StringPiece> kv = QuicTextUtils::Split(sp, ':'); |
319 base::SplitString(sp, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 314 QuicTextUtils::RemoveLeadingAndTrailingWhitespace(&kv[0]); |
320 CHECK_EQ(2u, kv.size()); | 315 QuicTextUtils::RemoveLeadingAndTrailingWhitespace(&kv[1]); |
321 string key; | 316 header_block[kv[0]] = kv[1]; |
322 base::TrimWhitespaceASCII(kv[0], base::TRIM_ALL, &key); | |
323 string value; | |
324 base::TrimWhitespaceASCII(kv[1], base::TRIM_ALL, &value); | |
325 request.extra_headers.SetHeader(key, value); | |
326 } | 317 } |
327 | 318 |
328 // Make sure to store the response, for later output. | 319 // Make sure to store the response, for later output. |
329 client.set_store_response(true); | 320 client.set_store_response(true); |
330 | 321 |
331 // Send the request. | 322 // Send the request. |
332 net::SpdyHeaderBlock header_block; | |
333 net::CreateSpdyHeadersFromHttpRequest(request, request.extra_headers, | |
334 /*direct=*/true, &header_block); | |
335 client.SendRequestAndWaitForResponse(header_block, body, /*fin=*/true); | 323 client.SendRequestAndWaitForResponse(header_block, body, /*fin=*/true); |
336 | 324 |
337 // Print request and response details. | 325 // Print request and response details. |
338 if (!FLAGS_quiet) { | 326 if (!FLAGS_quiet) { |
339 cout << "Request:" << endl; | 327 cout << "Request:" << endl; |
340 cout << "headers:" << header_block.DebugString(); | 328 cout << "headers:" << header_block.DebugString(); |
341 if (!FLAGS_body_hex.empty()) { | 329 if (!FLAGS_body_hex.empty()) { |
342 // Print the user provided hex, rather than binary body. | 330 // Print the user provided hex, rather than binary body. |
343 cout << "body:\n" | 331 cout << "body:\n" |
344 << QuicTextUtils::HexDump(QuicTextUtils::HexDecode(FLAGS_body_hex)) | 332 << QuicTextUtils::HexDump(QuicTextUtils::HexDecode(FLAGS_body_hex)) |
345 << endl; | 333 << endl; |
346 } else { | 334 } else { |
347 cout << "body: " << body << endl; | 335 cout << "body: " << body << endl; |
348 } | 336 } |
349 cout << endl; | 337 cout << endl; |
350 cout << "Response:" << endl; | 338 cout << "Response:" << endl; |
351 cout << "headers: " << client.latest_response_headers() << endl; | 339 cout << "headers: " << client.latest_response_headers() << endl; |
352 string response_body = client.latest_response_body(); | 340 string response_body = client.latest_response_body(); |
353 if (!FLAGS_body_hex.empty()) { | 341 if (!FLAGS_body_hex.empty()) { |
354 // Assume response is binary data. | 342 // Assume response is binary data. |
355 cout << "body:\n" << QuicTextUtils::HexDump(response_body) << endl; | 343 cout << "body:\n" << QuicTextUtils::HexDump(response_body) << endl; |
356 } else { | 344 } else { |
357 cout << "body: " << response_body << endl; | 345 cout << "body: " << response_body << endl; |
358 } | 346 } |
| 347 cout << "trailers: " << client.latest_response_trailers() << endl; |
359 } | 348 } |
360 | 349 |
361 size_t response_code = client.latest_response_code(); | 350 size_t response_code = client.latest_response_code(); |
362 if (response_code >= 200 && response_code < 300) { | 351 if (response_code >= 200 && response_code < 300) { |
363 cout << "Request succeeded (" << response_code << ")." << endl; | 352 cout << "Request succeeded (" << response_code << ")." << endl; |
364 return 0; | 353 return 0; |
365 } else if (response_code >= 300 && response_code < 400) { | 354 } else if (response_code >= 300 && response_code < 400) { |
366 if (FLAGS_redirect_is_success) { | 355 if (FLAGS_redirect_is_success) { |
367 cout << "Request succeeded (redirect " << response_code << ")." << endl; | 356 cout << "Request succeeded (redirect " << response_code << ")." << endl; |
368 return 0; | 357 return 0; |
369 } else { | 358 } else { |
370 cout << "Request failed (redirect " << response_code << ")." << endl; | 359 cout << "Request failed (redirect " << response_code << ")." << endl; |
371 return 1; | 360 return 1; |
372 } | 361 } |
373 } else { | 362 } else { |
374 cerr << "Request failed (" << response_code << ")." << endl; | 363 cerr << "Request failed (" << response_code << ")." << endl; |
375 return 1; | 364 return 1; |
376 } | 365 } |
377 } | 366 } |
OLD | NEW |