Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Side by Side Diff: net/tools/quic/quic_client_bin.cc

Issue 1545703002: If the user supplied a request body in hex, print response body in hex as well. In addition, conver… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@110403572
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | net/tools/quic/quic_simple_client_bin.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 345
330 // Send the request. 346 // Send the request.
331 net::SpdyHeaderBlock header_block = 347 net::SpdyHeaderBlock header_block =
332 net::tools::SpdyBalsaUtils::RequestHeadersToSpdyHeaders(headers); 348 net::tools::SpdyBalsaUtils::RequestHeadersToSpdyHeaders(headers);
333 client.SendRequestAndWaitForResponse(headers, body, /*fin=*/true); 349 client.SendRequestAndWaitForResponse(headers, body, /*fin=*/true);
334 350
335 // Print request and response details. 351 // Print request and response details.
336 if (!FLAGS_quiet) { 352 if (!FLAGS_quiet) {
337 cout << "Request:" << endl; 353 cout << "Request:" << endl;
338 cout << "headers:" << header_block.DebugString(); 354 cout << "headers:" << header_block.DebugString();
339 string body_to_print = body;
340 if (!FLAGS_body_hex.empty()) { 355 if (!FLAGS_body_hex.empty()) {
341 // Print the user provided hex, rather than binary body. 356 // Print the user provided hex, rather than binary body.
342 body_to_print = base::StringPrintf("(hex) 0x%s", FLAGS_body_hex.c_str()); 357 cout << "body hex: " << FLAGS_body_hex << endl;
358 string bytes;
359 DecodeHexString(FLAGS_body_hex, &bytes);
360 cout << "body ascii: " << BinaryToAscii(bytes) << endl;
361 } else {
362 cout << "body: " << body << endl;
343 } 363 }
344 cout << "body: " << body_to_print << endl;
345 cout << endl; 364 cout << endl;
346 cout << "Response:" << endl; 365 cout << "Response:" << endl;
347 cout << "headers: " << client.latest_response_headers() << endl; 366 cout << "headers: " << client.latest_response_headers() << endl;
348 cout << "body: " << client.latest_response_body() << endl; 367 string response_body = client.latest_response_body();
368 if (!FLAGS_body_hex.empty()) {
369 // Assume response is binary data.
370 string bytes;
371 DecodeHexString(response_body, &bytes);
372 cout << "body hex: " << bytes << endl;
373 cout << "body ascii: " << BinaryToAscii(response_body) << endl;
374 } else {
375 cout << "body: " << response_body << endl;
376 }
349 cout << "trailers: " << client.latest_response_trailers() << endl; 377 cout << "trailers: " << client.latest_response_trailers() << endl;
350 } 378 }
351 379
352 size_t response_code = client.latest_response_code(); 380 size_t response_code = client.latest_response_code();
353 if (response_code >= 200 && response_code < 300) { 381 if (response_code >= 200 && response_code < 300) {
354 cout << "Request succeeded (" << response_code << ")." << endl; 382 cout << "Request succeeded (" << response_code << ")." << endl;
355 return 0; 383 return 0;
356 } else if (response_code >= 300 && response_code < 400) { 384 } else if (response_code >= 300 && response_code < 400) {
357 if (FLAGS_redirect_is_success) { 385 if (FLAGS_redirect_is_success) {
358 cout << "Request succeeded (redirect " << response_code << ")." << endl; 386 cout << "Request succeeded (redirect " << response_code << ")." << endl;
359 return 0; 387 return 0;
360 } else { 388 } else {
361 cout << "Request failed (redirect " << response_code << ")." << endl; 389 cout << "Request failed (redirect " << response_code << ")." << endl;
362 return 1; 390 return 1;
363 } 391 }
364 } else { 392 } else {
365 cerr << "Request failed (" << response_code << ")." << endl; 393 cerr << "Request failed (" << response_code << ")." << endl;
366 return 1; 394 return 1;
367 } 395 }
368 } 396 }
OLDNEW
« no previous file with comments | « no previous file | net/tools/quic/quic_simple_client_bin.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698