From 4584ae9f8aa0e3739d92c85ff70b23a432847797 Mon Sep 17 00:00:00 2001 From: Andy Brody Date: Sun, 15 May 2016 23:08:43 -0400 Subject: [PATCH] Add redacted_uri, redacted_url methods. These make it easier for callers to access the password-free versions of the URI used by log_request. --- history.md | 2 ++ lib/restclient/request.rb | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/history.md b/history.md index 2a99e76..4a135bb 100644 --- a/history.md +++ b/history.md @@ -79,6 +79,8 @@ This release is largely API compatible, but makes several breaking changes. - Add a few more exception classes for obscure HTTP status codes. - Multipart: use a much more robust multipart boundary with greater entropy. - Make `RestClient::Payload::Base#inspect` stop pretending to be a String. +- Add `Request#redacted_uri` and `Request#redacted_url` to display the URI + with any password redacted. # 2.0.0.rc1 diff --git a/lib/restclient/request.rb b/lib/restclient/request.rb index 98a9e82..f011c91 100644 --- a/lib/restclient/request.rb +++ b/lib/restclient/request.rb @@ -426,20 +426,26 @@ module RestClient end end + def redacted_uri + if uri.password + sanitized_uri = uri.dup + sanitized_uri.password = 'REDACTED' + sanitized_uri + else + uri + end + end + + def redacted_url + redacted_uri.to_s + end + def log_request return unless RestClient.log - if uri.password - sanitized_uri = uri.dup - sanitized_uri.password = "REDACTED" - sanitized_url = sanitized_uri.to_s - else - sanitized_url = uri.to_s - end - out = [] - out << "RestClient.#{method} #{sanitized_url.inspect}" + out << "RestClient.#{method} #{redacted_url.inspect}" out << payload.short_inspect if payload out << processed_headers.to_a.sort.map { |(k, v)| [k.inspect, v.inspect].join("=>") }.join(", ") RestClient.log << out.join(', ') + "\n"