class Forward::Request

Attributes

body[RW]
headers[RW]
id[RW]
method[RW]
path[RW]
tunnel[RW]

Public Class Methods

new(tunnel, attributes) click to toggle source
# File lib/forward/request.rb, line 12
def initialize(tunnel, attributes)
  @tunnel      = tunnel
  @id          = attributes[:id]
  @method      = attributes[:method]
  @path        = attributes[:url]
  @headers     = attributes[:headers]
  @body        = ''
end

Public Instance Methods

<<(data) click to toggle source
# File lib/forward/request.rb, line 25
def <<(data)
  @body << data

  @body
end
destroy() click to toggle source
# File lib/forward/request.rb, line 31
def destroy
  @tunnel.requests.delete(@id)
end
handle_error(error) click to toggle source
# File lib/forward/request.rb, line 68
def handle_error(error)
  puts "\e[31mhttp://#{tunnel.authority} isn't responding, make sure your server is running on localhost.\e[0m"
  tunnel.socket.send(type: 'response:error', data: { id: id, error_type: 'localhost_unavailable' })

  destroy
end
process() click to toggle source
# File lib/forward/request.rb, line 35
def process
  options = {
    path: path,
    body: (body.empty? ? nil : body),
    head: headers
  }

  logger.debug "[request] #{method} #{url}#{path} #{options.inspect}"
  puts "[#{Time.now.strftime('%H:%M:%S')}] [#{method}] #{path}" unless Forward.quiet?

  http = EM::HttpRequest.new(url, :inactivity_timeout => 60).send(method.downcase, options)

  http.headers { |header|
    # It's no longer gzipped, clear it
    header.raw.delete('Content-Encoding') if header.raw.has_key?('Content-Encoding')
    tunnel.socket.send(type: 'response:start', data: { id: id, headers: header.raw, status: header.status })
  }

  http.stream { |chunk|
    tunnel.socket.send({type: 'response:data', data: { id: id }}, chunk)
  }

  http.callback {
    tunnel.socket.send(type: 'response:end', data: { id: id })
    destroy
  }

  http.errback {
    handle_error(http.error)
    destroy
  }
end
url() click to toggle source
# File lib/forward/request.rb, line 21
def url
  @url ||= "http://#{@tunnel.host}:#{@tunnel.port}"
end