Class: Forward::Request

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/forward/request.rb

Constant Summary

Constants included from Common

Common::EMAIL_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject

Returns the value of attribute body.



9
10
11
# File 'lib/forward/request.rb', line 9

def body
  @body
end

#headersObject

Returns the value of attribute headers.



10
11
12
# File 'lib/forward/request.rb', line 10

def headers
  @headers
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/forward/request.rb', line 6

def id
  @id
end

#methodObject

Returns the value of attribute method.



7
8
9
# File 'lib/forward/request.rb', line 7

def method
  @method
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/forward/request.rb', line 8

def path
  @path
end

#tunnelObject

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

#destroyObject



31
32
33
# File 'lib/forward/request.rb', line 31

def destroy
  @tunnel.requests.delete(@id)
end

#handle_error(error) ⇒ Object



65
66
67
68
69
70
# File 'lib/forward/request.rb', line 65

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

#processObject



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
# File 'lib/forward/request.rb', line 35

def process
  options = {
    path: path,
    body: (body.empty? ? nil : body),
    head: headers
  }

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

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

  http.headers { |header|
    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

#urlObject



21
22
23
# File 'lib/forward/request.rb', line 21

def url
  @url ||= "http://#{@tunnel.host}:#{@tunnel.port}"
end