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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 if (hex.empty()) | 127 if (hex.empty()) |
128 return true; | 128 return true; |
129 std::vector<uint8> v; | 129 std::vector<uint8> v; |
130 if (!base::HexStringToBytes(hex.as_string(), &v)) | 130 if (!base::HexStringToBytes(hex.as_string(), &v)) |
131 return false; | 131 return false; |
132 if (!v.empty()) | 132 if (!v.empty()) |
133 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size()); | 133 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size()); |
134 return true; | 134 return true; |
135 }; | 135 }; |
136 | 136 |
| 137 // Converts binary data into an ASCII string. Each character in the resulting |
| 138 // string is preceeded by a space, and replaced with a '.' if not printable. |
| 139 string BinaryToAscii(const string& binary) { |
| 140 string out = ""; |
| 141 for (const unsigned char c : binary) { |
| 142 // Leading space. |
| 143 out += " "; |
| 144 if (isprint(c)) { |
| 145 out += c; |
| 146 } else { |
| 147 out += '.'; |
| 148 } |
| 149 } |
| 150 return out; |
| 151 } |
| 152 |
137 int main(int argc, char *argv[]) { | 153 int main(int argc, char *argv[]) { |
138 base::CommandLine::Init(argc, argv); | 154 base::CommandLine::Init(argc, argv); |
139 base::CommandLine* line = base::CommandLine::ForCurrentProcess(); | 155 base::CommandLine* line = base::CommandLine::ForCurrentProcess(); |
140 const base::CommandLine::StringVector& urls = line->GetArgs(); | 156 const base::CommandLine::StringVector& urls = line->GetArgs(); |
141 | 157 |
142 logging::LoggingSettings settings; | 158 logging::LoggingSettings settings; |
143 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | 159 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
144 CHECK(logging::InitLogging(settings)); | 160 CHECK(logging::InitLogging(settings)); |
145 | 161 |
146 FLAGS_quic_supports_trailers = true; | 162 FLAGS_quic_supports_trailers = true; |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 net::SpdyHeaderBlock header_block; | 347 net::SpdyHeaderBlock header_block; |
332 net::CreateSpdyHeadersFromHttpRequest(request, request.extra_headers, | 348 net::CreateSpdyHeadersFromHttpRequest(request, request.extra_headers, |
333 net::HTTP2, /*direct=*/true, | 349 net::HTTP2, /*direct=*/true, |
334 &header_block); | 350 &header_block); |
335 client.SendRequestAndWaitForResponse(request, body, /*fin=*/true); | 351 client.SendRequestAndWaitForResponse(request, body, /*fin=*/true); |
336 | 352 |
337 // Print request and response details. | 353 // Print request and response details. |
338 if (!FLAGS_quiet) { | 354 if (!FLAGS_quiet) { |
339 cout << "Request:" << endl; | 355 cout << "Request:" << endl; |
340 cout << "headers:" << header_block.DebugString(); | 356 cout << "headers:" << header_block.DebugString(); |
341 string body_to_print = body; | |
342 if (!FLAGS_body_hex.empty()) { | 357 if (!FLAGS_body_hex.empty()) { |
343 // Print the user provided hex, rather than binary body. | 358 // Print the user provided hex, rather than binary body. |
344 body_to_print = base::StringPrintf("(hex) 0x%s", FLAGS_body_hex.c_str()); | 359 cout << "body hex: " << FLAGS_body_hex << endl; |
| 360 string bytes; |
| 361 DecodeHexString(FLAGS_body_hex, &bytes); |
| 362 cout << "body ascii: " << BinaryToAscii(bytes) << endl; |
| 363 } else { |
| 364 cout << "body: " << body << endl; |
345 } | 365 } |
346 cout << "body: " << body_to_print << endl; | |
347 cout << endl; | 366 cout << endl; |
348 cout << "Response:" << endl; | 367 cout << "Response:" << endl; |
349 cout << "headers: " << client.latest_response_headers() << endl; | 368 cout << "headers: " << client.latest_response_headers() << endl; |
350 cout << "body: " << client.latest_response_body() << endl; | 369 string response_body = client.latest_response_body(); |
| 370 if (!FLAGS_body_hex.empty()) { |
| 371 // Assume response is binary data. |
| 372 string bytes; |
| 373 DecodeHexString(response_body, &bytes); |
| 374 cout << "body hex: " << bytes << endl; |
| 375 cout << "body ascii: " << BinaryToAscii(response_body) << endl; |
| 376 } else { |
| 377 cout << "body: " << response_body << endl; |
| 378 } |
351 } | 379 } |
352 | 380 |
353 size_t response_code = client.latest_response_code(); | 381 size_t response_code = client.latest_response_code(); |
354 if (response_code >= 200 && response_code < 300) { | 382 if (response_code >= 200 && response_code < 300) { |
355 cout << "Request succeeded (" << response_code << ")." << endl; | 383 cout << "Request succeeded (" << response_code << ")." << endl; |
356 return 0; | 384 return 0; |
357 } else if (response_code >= 300 && response_code < 400) { | 385 } else if (response_code >= 300 && response_code < 400) { |
358 if (FLAGS_redirect_is_success) { | 386 if (FLAGS_redirect_is_success) { |
359 cout << "Request succeeded (redirect " << response_code << ")." << endl; | 387 cout << "Request succeeded (redirect " << response_code << ")." << endl; |
360 return 0; | 388 return 0; |
361 } else { | 389 } else { |
362 cout << "Request failed (redirect " << response_code << ")." << endl; | 390 cout << "Request failed (redirect " << response_code << ")." << endl; |
363 return 1; | 391 return 1; |
364 } | 392 } |
365 } else { | 393 } else { |
366 cerr << "Request failed (" << response_code << ")." << endl; | 394 cerr << "Request failed (" << response_code << ")." << endl; |
367 return 1; | 395 return 1; |
368 } | 396 } |
369 } | 397 } |
OLD | NEW |