Class: Nonnative::HTTPProxy

Inherits:
HTTPService
  • Object
show all
Defined in:
lib/nonnative/http_proxy_server.rb

Overview

HTTP service implementing a simple forward proxy.

The upstream host is configured via service settings (see HTTPProxyServer).

Supported HTTP verbs: GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS.

Constant Summary collapse

NON_FORWARDABLE_RESPONSE_HEADERS =
%w[
  connection
  content-encoding
  content-length
  keep-alive
  location
  proxy-authenticate
  proxy-authorization
  proxy-authentication-info
  proxy-connection
  set-cookie
  status
  te
  trailer
  transfer-encoding
  upgrade
].freeze
NON_FORWARDABLE_REQUEST_HEADERS =
%w[
  Host
  Accept-Encoding
  Version
  Proxy-Authenticate
  Proxy-Authorization
].freeze

Instance Method Summary collapse

Instance Method Details

#api_response(method:, url:, headers:, payload: nil) ⇒ RestClient::Response

Executes the upstream request and returns the response.

Parameters:

  • method (Symbol)

    HTTP verb name (e.g. :get)

  • url (String)

    upstream URL

  • headers (Hash)

    request headers

  • payload (String, nil) (defaults to: nil)

    request payload

Returns:

  • (RestClient::Response)

    response for error statuses, otherwise RestClient return value



85
86
87
88
89
90
91
92
# File 'lib/nonnative/http_proxy_server.rb', line 85

def api_response(method:, url:, headers:, payload: nil)
  options = { method:, url:, headers: }
  options[:payload] = payload unless payload.nil?

  RestClient::Request.execute(options)
rescue RestClient::Exception => e
  e.response
end

#build_url(request, settings) ⇒ String

Builds the upstream URL for the given request.

Parameters:

  • request (Sinatra::Request)

    the incoming request

  • settings (Sinatra::Base)

    Sinatra settings, expected to include host

Returns:

  • (String)

    HTTPS URL for the upstream request



74
75
76
# File 'lib/nonnative/http_proxy_server.rb', line 74

def build_url(request, settings)
  URI::HTTPS.build(host: settings.host, path: request.path_info, query: request.query_string).to_s
end

#forward_request_headers(request) ⇒ Hash{String=>String}

Extracts request headers from the Rack environment and normalizes them to standard HTTP names.

Certain hop-by-hop or proxy-specific headers are removed.

Parameters:

  • request (Sinatra::Request)

    the incoming request

Returns:

  • (Hash{String=>String})

    headers to forward to the upstream



59
60
61
62
63
64
65
66
67
# File 'lib/nonnative/http_proxy_server.rb', line 59

def forward_request_headers(request)
  headers = request.env.each_with_object({}) do |(header, value), result|
    next unless forward_request_header?(header)

    result[normalized_request_header_name(header)] = value
  end

  headers.except(*NON_FORWARDABLE_REQUEST_HEADERS)
end

#retrieve_payload(request, verb) ⇒ String?

Extracts the request payload for verbs that can carry a body.

Parameters:

  • request (Sinatra::Request)

    the incoming request

  • verb (String)

    HTTP verb name (e.g. "post")

Returns:

  • (String, nil)

    request payload for body-carrying verbs



99
100
101
102
103
104
# File 'lib/nonnative/http_proxy_server.rb', line 99

def retrieve_payload(request, verb)
  return unless %w[post put patch delete].include?(verb)

  payload = request.body.read
  payload unless payload.empty?
end