Class: Nonnative::HTTPProxy
- Inherits:
-
HTTPService
- Object
- HTTPService
- Nonnative::HTTPProxy
- 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
-
#api_response(method:, url:, headers:, payload: nil) ⇒ RestClient::Response
Executes the upstream request and returns the response.
-
#build_url(request, settings) ⇒ String
Builds the upstream URL for the given request.
-
#forward_request_headers(request) ⇒ Hash{String=>String}
Extracts request headers from the Rack environment and normalizes them to standard HTTP names.
-
#retrieve_payload(request, verb) ⇒ String?
Extracts the request payload for verbs that can carry a body.
Instance Method Details
#api_response(method:, url:, headers:, payload: nil) ⇒ RestClient::Response
Executes the upstream request and returns the response.
88 89 90 91 92 93 94 95 |
# File 'lib/nonnative/http_proxy_server.rb', line 88 def api_response(method:, url:, headers:, payload: nil) = { method:, url:, headers: } [:payload] = payload unless payload.nil? RestClient::Request.execute() rescue RestClient::Exception => e e.response end |
#build_url(request, settings) ⇒ String
Builds the upstream URL for the given request.
75 76 77 78 79 |
# File 'lib/nonnative/http_proxy_server.rb', line 75 def build_url(request, settings) uri_class = settings.scheme == 'http' ? URI::HTTP : URI::HTTPS uri_class.build(host: settings.host, port: settings.port, 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.
60 61 62 63 64 65 66 67 68 |
# File 'lib/nonnative/http_proxy_server.rb', line 60 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.
102 103 104 105 106 107 |
# File 'lib/nonnative/http_proxy_server.rb', line 102 def retrieve_payload(request, verb) return unless %w[post put patch delete].include?(verb) payload = request.body.read payload unless payload.empty? end |