Class: Cyperful::UiServer::ReverseProxy

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/cyperful/ui_server.rb

Overview

super naive reverse proxy

Instance Method Summary collapse

Constructor Details

#initialize(server, config = {}) ⇒ ReverseProxy

Returns a new instance of ReverseProxy.



152
153
154
155
156
# File 'lib/cyperful/ui_server.rb', line 152

def initialize(server, config = {})
  super
  @target_url = config.fetch(:target_url)
  @forward_headers = config[:forward_headers] || ["accept"]
end

Instance Method Details

#do_GET(request, response) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cyperful/ui_server.rb', line 158

def do_GET(request, response)
  # Target server URL
  target_uri = URI(@target_url)
  target_uri.path = request.path
  target_uri.query = request.query_string if request.query_string

  # Create a new request to the target server with the original request path
  target_request = Net::HTTP::Get.new(target_uri)
  @forward_headers.each do |header|
    target_request[header] = request[header]
  end

  # Send the request to the target server
  target_response =
    Net::HTTP.start(target_uri.host, target_uri.port) do |http|
      http.request(target_request)
    end

  # Set the response from the target server as the response to send back to the client
  response.status = target_response.code.to_i
  response["Content-Type"] = target_response["content-type"]
  response.body = target_response.body
end