Skip to content

Commit 9647f8d

Browse files
committed
DRY up HTTP request code.
1 parent ebcf692 commit 9647f8d

File tree

1 file changed

+21
-34
lines changed
  • lib/metasploit/framework/data_service/remote/http

1 file changed

+21
-34
lines changed

lib/metasploit/framework/data_service/remote/http/core.rb

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RemoteHTTPDataService
1515
include DataServiceAutoLoader
1616

1717
ONLINE_TEST_URL = "/api/1/msf/online"
18-
EXEC_ASYNC = {:exec_async => true}
18+
EXEC_ASYNC = { :exec_async => true }
1919
GET_REQUEST = 'GET'
2020
POST_REQUEST = 'POST'
2121

@@ -32,7 +32,7 @@ def initialize(endpoint)
3232
# POST data and don't wait for the endpoint to process the data before getting a response
3333
#
3434
def post_data_async(path, data_hash)
35-
post_data(path, data_hash.merge(EXEC_ASYNC))
35+
make_request(POST_REQUEST, path, data_hash.merge(EXEC_ASYNC))
3636
end
3737

3838
#
@@ -44,30 +44,7 @@ def post_data_async(path, data_hash)
4444
# @return A wrapped response (ResponseWrapper), see below.
4545
#
4646
def post_data(path, data_hash)
47-
begin
48-
raise 'Data to post to remote service cannot be null or empty' if (data_hash.nil? || data_hash.empty?)
49-
50-
puts "#{Time.now} - Posting #{data_hash} to #{path}"
51-
client = @client_pool.pop()
52-
request = Net::HTTP::Post.new(path)
53-
request = build_request(request, data_hash)
54-
response = client.request(request)
55-
56-
if response.code == "200"
57-
#puts "POST request: #{path} with body: #{json_body} sent successfully"
58-
return SuccessResponse.new(response)
59-
else
60-
puts "POST request: #{path} with body: #{request.body} failed with code: #{response.code} message: #{response.body}"
61-
return FailedResponse.new(response)
62-
end
63-
rescue Exception => e
64-
puts "Problem with POST request: #{e.message}"
65-
e.backtrace.each do |line|
66-
puts "#{line}\n"
67-
end
68-
ensure
69-
@client_pool << client
70-
end
47+
make_request(POST_REQUEST, path, data_hash)
7148
end
7249

7350
#
@@ -79,24 +56,34 @@ def post_data(path, data_hash)
7956
# @return A wrapped response (ResponseWrapper), see below.
8057
#
8158
def get_data(path, data_hash = nil)
82-
begin
59+
make_request(GET_REQUEST, path, data_hash)
60+
end
8361

84-
puts "#{Time.now} - Getting #{path} with #{data_hash ? data_hash : "nil"}"
62+
def make_request(request_type, path, data_hash = nil)
63+
begin
64+
puts "#{Time.now} - HTTP #{request_type} request to #{path} with #{data_hash ? data_hash : "nil"}"
8565
client = @client_pool.pop()
86-
request = Net::HTTP::Get.new(path)
87-
request = build_request(request, data_hash)
88-
response = client.request(request)
66+
case request_type
67+
when GET_REQUEST
68+
request = Net::HTTP::Get.new(path)
69+
when POST_REQUEST
70+
request = Net::HTTP::Post.new(path)
71+
else
72+
raise Exception, 'A request_type must be specified'
73+
end
74+
built_request = build_request(request, data_hash)
75+
response = client.request(built_request)
8976

9077
if response.code == "200"
9178
# puts 'request sent successfully'
9279
return SuccessResponse.new(response)
9380
else
94-
puts "GET request: #{path} failed with code: #{response.code} message: #{response.body}"
81+
puts "HTTP #{request_type} request: #{path} failed with code: #{response.code} message: #{response.body}"
9582
return FailedResponse.new(response)
9683
end
9784
rescue Exception => e
98-
puts "Problem with GET request: #{e.message}"
99-
e.backtrace.each {|line| puts "#{line}\n"}
85+
puts "Problem with HTTP #{request_type} request: #{e.message}"
86+
e.backtrace.each { |line| puts "#{line}\n" }
10087
ensure
10188
@client_pool << client
10289
end

0 commit comments

Comments
 (0)