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 "base/strings/stringprintf.h" | |
51 #include "net/base/net_errors.h" | 47 #include "net/base/net_errors.h" |
52 #include "net/base/privacy_mode.h" | 48 #include "net/base/privacy_mode.h" |
53 #include "net/cert/cert_verifier.h" | 49 #include "net/cert/cert_verifier.h" |
54 #include "net/cert/multi_log_ct_verifier.h" | 50 #include "net/cert/multi_log_ct_verifier.h" |
55 #include "net/http/transport_security_state.h" | 51 #include "net/http/transport_security_state.h" |
56 #include "net/quic/chromium/crypto/proof_verifier_chromium.h" | 52 #include "net/quic/chromium/crypto/proof_verifier_chromium.h" |
57 #include "net/quic/core/quic_flags.h" | 53 #include "net/quic/core/quic_flags.h" |
58 #include "net/quic/core/quic_packets.h" | 54 #include "net/quic/core/quic_packets.h" |
59 #include "net/quic/core/quic_server_id.h" | 55 #include "net/quic/core/quic_server_id.h" |
60 #include "net/quic/core/quic_utils.h" | 56 #include "net/quic/core/quic_utils.h" |
61 #include "net/quic/platform/api/quic_socket_address.h" | 57 #include "net/quic/platform/api/quic_socket_address.h" |
62 #include "net/quic/platform/api/quic_str_cat.h" | 58 #include "net/quic/platform/api/quic_str_cat.h" |
| 59 #include "net/quic/platform/api/quic_text_utils.h" |
63 #include "net/spdy/spdy_header_block.h" | 60 #include "net/spdy/spdy_header_block.h" |
64 #include "net/tools/epoll_server/epoll_server.h" | 61 #include "net/tools/epoll_server/epoll_server.h" |
65 #include "net/tools/quic/quic_client.h" | 62 #include "net/tools/quic/quic_client.h" |
66 #include "net/tools/quic/synchronous_host_resolver.h" | 63 #include "net/tools/quic/synchronous_host_resolver.h" |
67 #include "url/gurl.h" | 64 #include "url/gurl.h" |
68 | 65 |
69 using base::StringPiece; | 66 using base::StringPiece; |
70 using net::CertVerifier; | 67 using net::CertVerifier; |
71 using net::CTPolicyEnforcer; | 68 using net::CTPolicyEnforcer; |
72 using net::CTVerifier; | 69 using net::CTVerifier; |
73 using net::MultiLogCTVerifier; | 70 using net::MultiLogCTVerifier; |
74 using net::ProofVerifier; | 71 using net::ProofVerifier; |
75 using net::ProofVerifierChromium; | 72 using net::ProofVerifierChromium; |
76 using net::SpdyHeaderBlock; | 73 using net::SpdyHeaderBlock; |
77 using net::TransportSecurityState; | 74 using net::TransportSecurityState; |
| 75 using net::QuicTextUtils; |
78 using std::cout; | 76 using std::cout; |
79 using std::cerr; | 77 using std::cerr; |
80 using std::string; | 78 using std::string; |
81 using std::endl; | 79 using std::endl; |
82 | 80 |
83 // The IP or hostname the quic client will connect to. | 81 // The IP or hostname the quic client will connect to. |
84 string FLAGS_host = ""; | 82 string FLAGS_host = ""; |
85 // The port to connect to. | 83 // The port to connect to. |
86 int32_t FLAGS_port = 0; | 84 int32_t FLAGS_port = 0; |
87 // If set, send a POST with this body. | 85 // If set, send a POST with this body. |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 cerr << "Failed to connect to " << host_port | 290 cerr << "Failed to connect to " << host_port |
293 << ". Error: " << net::QuicErrorCodeToString(error) << endl; | 291 << ". Error: " << net::QuicErrorCodeToString(error) << endl; |
294 return 1; | 292 return 1; |
295 } | 293 } |
296 cout << "Connected to " << host_port << endl; | 294 cout << "Connected to " << host_port << endl; |
297 | 295 |
298 // Construct the string body from flags, if provided. | 296 // Construct the string body from flags, if provided. |
299 string body = FLAGS_body; | 297 string body = FLAGS_body; |
300 if (!FLAGS_body_hex.empty()) { | 298 if (!FLAGS_body_hex.empty()) { |
301 DCHECK(FLAGS_body.empty()) << "Only set one of --body and --body_hex."; | 299 DCHECK(FLAGS_body.empty()) << "Only set one of --body and --body_hex."; |
302 body = net::QuicUtils::HexDecode(FLAGS_body_hex); | 300 body = QuicTextUtils::HexDecode(FLAGS_body_hex); |
303 } | 301 } |
304 | 302 |
305 // Construct a GET or POST request for supplied URL. | 303 // Construct a GET or POST request for supplied URL. |
306 SpdyHeaderBlock header_block; | 304 SpdyHeaderBlock header_block; |
307 header_block[":method"] = body.empty() ? "GET" : "POST"; | 305 header_block[":method"] = body.empty() ? "GET" : "POST"; |
308 header_block[":scheme"] = url.scheme(); | 306 header_block[":scheme"] = url.scheme(); |
309 header_block[":authority"] = url.host(); | 307 header_block[":authority"] = url.host(); |
310 header_block[":path"] = url.path(); | 308 header_block[":path"] = url.path(); |
311 | 309 |
312 // Append any additional headers supplied on the command line. | 310 // Append any additional headers supplied on the command line. |
313 for (const std::string& header : | 311 for (StringPiece sp : QuicTextUtils::Split(FLAGS_headers, ';')) { |
314 base::SplitString(FLAGS_headers, ";", base::KEEP_WHITESPACE, | 312 QuicTextUtils::RemoveLeadingAndTrailingWhitespace(&sp); |
315 base::SPLIT_WANT_NONEMPTY)) { | |
316 string sp; | |
317 base::TrimWhitespaceASCII(header, base::TRIM_ALL, &sp); | |
318 if (sp.empty()) { | 313 if (sp.empty()) { |
319 continue; | 314 continue; |
320 } | 315 } |
321 std::vector<string> kv = | 316 std::vector<StringPiece> kv = QuicTextUtils::Split(sp, ':'); |
322 base::SplitString(sp, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 317 QuicTextUtils::RemoveLeadingAndTrailingWhitespace(&kv[0]); |
323 CHECK_EQ(2u, kv.size()); | 318 QuicTextUtils::RemoveLeadingAndTrailingWhitespace(&kv[1]); |
324 string key; | |
325 base::TrimWhitespaceASCII(kv[0], base::TRIM_ALL, &key); | |
326 string value; | |
327 base::TrimWhitespaceASCII(kv[1], base::TRIM_ALL, &value); | |
328 header_block[kv[0]] = kv[1]; | 319 header_block[kv[0]] = kv[1]; |
329 } | 320 } |
330 | 321 |
331 // Make sure to store the response, for later output. | 322 // Make sure to store the response, for later output. |
332 client.set_store_response(true); | 323 client.set_store_response(true); |
333 // Send the request. | 324 // Send the request. |
334 client.SendRequestAndWaitForResponse(header_block, body, /*fin=*/true); | 325 client.SendRequestAndWaitForResponse(header_block, body, /*fin=*/true); |
335 | 326 |
336 // Print request and response details. | 327 // Print request and response details. |
337 if (!FLAGS_quiet) { | 328 if (!FLAGS_quiet) { |
338 cout << "Request:" << endl; | 329 cout << "Request:" << endl; |
339 cout << "headers:" << header_block.DebugString(); | 330 cout << "headers:" << header_block.DebugString(); |
340 if (!FLAGS_body_hex.empty()) { | 331 if (!FLAGS_body_hex.empty()) { |
341 // Print the user provided hex, rather than binary body. | 332 // Print the user provided hex, rather than binary body. |
342 cout << "body:\n" | 333 cout << "body:\n" |
343 << net::QuicUtils::HexDump(net::QuicUtils::HexDecode(FLAGS_body_hex)) | 334 << QuicTextUtils::HexDump(QuicTextUtils::HexDecode(FLAGS_body_hex)) |
344 << endl; | 335 << endl; |
345 } else { | 336 } else { |
346 cout << "body: " << body << endl; | 337 cout << "body: " << body << endl; |
347 } | 338 } |
348 cout << endl; | 339 cout << endl; |
349 cout << "Response:" << endl; | 340 cout << "Response:" << endl; |
350 cout << "headers: " << client.latest_response_headers() << endl; | 341 cout << "headers: " << client.latest_response_headers() << endl; |
351 string response_body = client.latest_response_body(); | 342 string response_body = client.latest_response_body(); |
352 if (!FLAGS_body_hex.empty()) { | 343 if (!FLAGS_body_hex.empty()) { |
353 // Assume response is binary data. | 344 // Assume response is binary data. |
354 cout << "body:\n" << net::QuicUtils::HexDump(response_body) << endl; | 345 cout << "body:\n" << QuicTextUtils::HexDump(response_body) << endl; |
355 } else { | 346 } else { |
356 cout << "body: " << response_body << endl; | 347 cout << "body: " << response_body << endl; |
357 } | 348 } |
358 cout << "trailers: " << client.latest_response_trailers() << endl; | 349 cout << "trailers: " << client.latest_response_trailers() << endl; |
359 } | 350 } |
360 | 351 |
361 size_t response_code = client.latest_response_code(); | 352 size_t response_code = client.latest_response_code(); |
362 if (response_code >= 200 && response_code < 300) { | 353 if (response_code >= 200 && response_code < 300) { |
363 cout << "Request succeeded (" << response_code << ")." << endl; | 354 cout << "Request succeeded (" << response_code << ")." << endl; |
364 return 0; | 355 return 0; |
365 } else if (response_code >= 300 && response_code < 400) { | 356 } else if (response_code >= 300 && response_code < 400) { |
366 if (FLAGS_redirect_is_success) { | 357 if (FLAGS_redirect_is_success) { |
367 cout << "Request succeeded (redirect " << response_code << ")." << endl; | 358 cout << "Request succeeded (redirect " << response_code << ")." << endl; |
368 return 0; | 359 return 0; |
369 } else { | 360 } else { |
370 cout << "Request failed (redirect " << response_code << ")." << endl; | 361 cout << "Request failed (redirect " << response_code << ")." << endl; |
371 return 1; | 362 return 1; |
372 } | 363 } |
373 } else { | 364 } else { |
374 cerr << "Request failed (" << response_code << ")." << endl; | 365 cerr << "Request failed (" << response_code << ")." << endl; |
375 return 1; | 366 return 1; |
376 } | 367 } |
377 } | 368 } |
OLD | NEW |