class NessusClient::Request

Abstract http request class for NessusClient. Provides some helper methods for perform HTTP requests.

Constants

DEFAULT_HEADERS

Default HTTP header to be used on the requests.

Attributes

url[R]

@return [String] The base url of the API.

Public Class Methods

new(params = {}) click to toggle source

@param [Hash] params the options to create a NessusClient::Request with. @option params [String] :uri ('localhost:8834/') Nessus server to connect with @option params [String] :ssl_verify_peer (true) Whether should check valid SSL certificate

# File lib/nessus_client/request.rb, line 22
def initialize(params = {})
  params = { uri: nil }.merge(params)
  @ssl_verify_peer = params[:ssl_verify_peer] ? true : false
  @url = NessusClient::Request.uri_parse(params.fetch(:uri))
end
uri_parse(uri) click to toggle source

Parse a receiveid string against the URI stantard. @param [String] uri A string to be validate URI. @return [String] A string uri.

# File lib/nessus_client/request.rb, line 71
def self.uri_parse(uri)
  url = URI.parse(uri)
  raise URI::InvalidURIError unless url.scheme

  url.to_s
end

Public Instance Methods

delete(opts = {}) click to toggle source

Perform a HTTP DELETE request. @param [Hash] opts to use in the request. @option opts [String] path The URI path to perform the request. @option opts [String] payload The HTTP body to send. @option opts [String] query The URI query to send. @return [Hash, String] The body of the resposnse if there is any.

# File lib/nessus_client/request.rb, line 64
def delete(opts = {})
  http_request(opts, :delete)
end
get(opts = {}) click to toggle source

Perform a HTTP GET request. @param [Hash] opts to use in the request. @option opts [String] path The URI path to perform the request. @option opts [String] payload The HTTP body to send. @option opts [String] query The URI query to send. @return [Hash, String] The body of the resposnse if there is any.

# File lib/nessus_client/request.rb, line 34
def get(opts = {})
  http_request(opts, :get)
end
post(opts = {}) click to toggle source

Perform a HTTP POST request. @param [Hash] opts to use in the request. @option opts [String] path The URI path to perform the request. @option opts [String] payload The HTTP body to send. @option opts [String] query The URI query to send. @return [Hash, String] The body of the resposnse if there is any.

# File lib/nessus_client/request.rb, line 44
def post(opts = {})
  http_request(opts, :post)
end
put(opts = {}) click to toggle source

Perform a HTTP PUT request. @param [Hash] opts to use in the request. @option opts [String] path The URI path to perform the request. @option opts [String] payload The HTTP body to send. @option opts [String] query The URI query to send. @return [Hash, String] The body of the resposnse if there is any.

# File lib/nessus_client/request.rb, line 54
def put(opts = {})
  http_request(opts, :put)
end

Private Instance Methods

http_request(args, method = :get) click to toggle source

@private HTTP request abstraction to be used. @param [Symbol] method The HTTP method to be used on the request. @param [Hash] args Parameters to use in the request. @option args [String] path (nil) The URI path to perform the request. @option args [String] payload (nil) The HTTP body to send. @option args [String] query (nil) The URI query to send. @option args [String] headers (nil) The headers to send. @return [Hash, String] The body of the resposnse if there is any.

# File lib/nessus_client/request.rb, line 88
def http_request(args, method = :get)
  opts = { path: nil, payload: nil, query: nil, headers: nil }.merge(args)
  connection = Excon.new(@url, { ssl_verify_peer: @ssl_verify_peer })
  body = opts[:payload] ? Oj.dump(opts[:payload], mode: :compat) : ''
  options = {
    method: method,
    path: opts.fetch(:path),
    body: body,
    query: opts.fetch(:query),
    headers: opts.fetch(:headers),
    expects: [200, 201]
  }
  response = connection.request(options)
  ret = Oj.load(response.body) # if response.body.length > 0
rescue Oj::ParseError
  response.body
else
  ret
end