Class: Forward::Request
- Inherits:
-
Object
- Object
- Forward::Request
- Includes:
- Common
- Defined in:
- lib/forward/request.rb
Constant Summary
Constants included from Common
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#id ⇒ Object
Returns the value of attribute id.
-
#method ⇒ Object
Returns the value of attribute method.
-
#path ⇒ Object
Returns the value of attribute path.
-
#tunnel ⇒ Object
Returns the value of attribute tunnel.
Instance Method Summary collapse
- #<<(data) ⇒ Object
- #destroy ⇒ Object
- #handle_error(error) ⇒ Object
-
#initialize(tunnel, attributes) ⇒ Request
constructor
A new instance of Request.
- #process ⇒ Object
- #url ⇒ Object
Methods included from Common
#config, #exit_with_error, #exit_with_message, #logged_in?, #logger, #os, #stop_reactor_and_exit, #windows?
Constructor Details
#initialize(tunnel, attributes) ⇒ Request
Returns a new instance of Request.
12 13 14 15 16 17 18 19 |
# 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 |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
9 10 11 |
# File 'lib/forward/request.rb', line 9 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
10 11 12 |
# File 'lib/forward/request.rb', line 10 def headers @headers end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/forward/request.rb', line 6 def id @id end |
#method ⇒ Object
Returns the value of attribute method.
7 8 9 |
# File 'lib/forward/request.rb', line 7 def method @method end |
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/forward/request.rb', line 8 def path @path end |
#tunnel ⇒ Object
Returns the value of attribute tunnel.
5 6 7 |
# File 'lib/forward/request.rb', line 5 def tunnel @tunnel end |
Instance Method Details
#<<(data) ⇒ Object
25 26 27 28 29 |
# File 'lib/forward/request.rb', line 25 def <<(data) @body << data @body end |
#destroy ⇒ Object
31 32 33 |
# File 'lib/forward/request.rb', line 31 def destroy @tunnel.requests.delete(@id) end |
#handle_error(error) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/forward/request.rb', line 68 def handle_error(error) puts "\e[31mhttp://#{tunnel.} 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 ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/forward/request.rb', line 35 def process = { path: path, body: (body.empty? ? nil : body), head: headers } logger.debug "[request] #{method} #{url}#{path} #{.inspect}" puts "[#{Time.now.strftime('%H:%M:%S')}] [#{method}] #{path}" unless Forward.quiet? http = EM::HttpRequest.new(url, :inactivity_timeout => 60).send(method.downcase, ) 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 ⇒ Object
21 22 23 |
# File 'lib/forward/request.rb', line 21 def url @url ||= "http://#{@tunnel.host}:#{@tunnel.port}" end |