Class: Nonnative::HTTPProxy
- Inherits:
-
Sinatra::Application
- Object
- Sinatra::Application
- Nonnative::HTTPProxy
- Defined in:
- lib/nonnative/http_proxy_server.rb
Overview
Sinatra application implementing a simple forward proxy.
The upstream host is configured via Sinatra settings (see HTTPProxyServer).
Supported HTTP verbs: GET, POST, PUT, PATCH, DELETE.
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.
-
#retrieve_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.
57 58 59 60 61 62 63 64 |
# File 'lib/nonnative/http_proxy_server.rb', line 57 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.
47 48 49 |
# File 'lib/nonnative/http_proxy_server.rb', line 47 def build_url(request, settings) URI::HTTPS.build(host: settings.host, path: request.path_info, query: request.query_string).to_s end |
#retrieve_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.
32 33 34 35 36 37 38 39 40 |
# File 'lib/nonnative/http_proxy_server.rb', line 32 def retrieve_headers(request) headers = request.env.each_with_object({}) do |(header, value), result| next unless forward_header?(header) result[normalized_header_name(header)] = value end headers.except('Host', 'Accept-Encoding', 'Version') end |
#retrieve_payload(request, verb) ⇒ String?
Extracts the request payload for verbs that can carry a body.
71 72 73 74 75 76 |
# File 'lib/nonnative/http_proxy_server.rb', line 71 def retrieve_payload(request, verb) return unless %w[post put patch delete].include?(verb) payload = request.body.read payload unless payload.empty? end |