Skip to content

Commit

Permalink
#186918768 : support rest-client where it override standard stream ob…
Browse files Browse the repository at this point in the history
…ject (#55)

* support rest-client where it override stream object with a "payload" that does not have rewind

rest-client hasn't been updated since August 2019,

Rest client has this issue (where it override the standard stream object) with custom payload object that does NOT implement standard "rewind" method.

rest-client/rest-client#681

But this fix will work around that.

* remove comment
  • Loading branch information
xinghengwang authored Jan 29, 2024
1 parent d6eefc8 commit f5eb4da
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/moesif_rack/moesif_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(app, options = {})
@app = app
raise 'application_id required for Moesif Middleware' unless options['application_id']

@api_client = MoesifApi::MoesifAPIClient.new(options['application_id'], 'moesif-rack/2.2.2')
@api_client = MoesifApi::MoesifAPIClient.new(options['application_id'], 'moesif-rack/2.2.3')
@api_controller = @api_client.api

@api_version = options['api_version']
Expand Down
22 changes: 20 additions & 2 deletions moesif_capture_outgoing/httplog/adapters/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,33 @@ module Net
class HTTP
alias orig_request request unless method_defined?(:orig_request)

def extract_body_from_body_stream(body_stream)
begin
result = nil
if body_stream.respond_to?(:rewind) and body_stream.respond_to?(:read)
result = body_stream.read
body_stream.rewind
elsif body_stream.respond_to?(:to_s)
result = body_stream.to_s
if body_stream.respond_to?(:seek)
# if stream respond to seek let's reset seek to 0
body_stream.seek(0)
end
end
result
rescue StandardError => e
return nil
end
end

def request(request, body = nil, &block)
# Request Start Time
request_time = Time.now.utc.iso8601(3)
# URL
url = "https://#{@address}#{request.path}"

if (not request.body_stream.nil?) && MoesifCaptureOutgoing.should_capture_body
req_body_from_stream = request.body_stream.read
request.body_stream.rewind
req_body_from_stream = extract_body_from_body_stream(request.body_stream)
end

# Response
Expand Down
10 changes: 9 additions & 1 deletion moesif_capture_outgoing/httplog/http_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ def send_moesif_event(url, request, request_time, response, response_time, body_
req_content_type = req_headers['content-type'].nil? ? req_headers['Content-Type'] : req_headers['content-type']

# Request Body
req_body_string = request.body.nil? || request.body.empty? ? body_from_req_call : request.body
req_body_string = nil
if not (request.body.nil? || request.body.empty?)
req_body_string = request.body;
elsif not body_from_req_call.nil?
req_body_string = body_from_req_call
elsif req_body_from_stream.is_a? String
req_body_string = req_body_from_stream
end

req_body_transfer_encoding = nil
req_body = nil

Expand Down
2 changes: 1 addition & 1 deletion moesif_rack.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'moesif_rack'
s.version = '2.2.2'
s.version = '2.2.3'
s.summary = 'moesif_rack'
s.description = 'Rack/Rails middleware to log API calls to Moesif API analytics and monitoring'
s.authors = ['Moesif, Inc', 'Xing Wang']
Expand Down

0 comments on commit f5eb4da

Please sign in to comment.