Class: Http::Min

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/minhttp.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



15
16
17
# File 'lib/minhttp.rb', line 15

def callback
  @callback
end

#hostObject

Returns the value of attribute host.



15
16
17
# File 'lib/minhttp.rb', line 15

def host
  @host
end

#request_dataObject

Returns the value of attribute request_data.



15
16
17
# File 'lib/minhttp.rb', line 15

def request_data
  @request_data
end

#sslObject

Returns the value of attribute ssl.



15
16
17
# File 'lib/minhttp.rb', line 15

def ssl
  @ssl
end

Class Method Details

.configure(options = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/minhttp.rb', line 21

def self.configure(options={})
  @@options = options
  @@logger = options[:logger] || Logger.new(STDOUT)
  @@connections = 0
  Http::SSLValidator.configure(options[:ssl_logger])
end

.configured?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/minhttp.rb', line 28

def self.configured?
  class_variable_defined?("@@logger")
end

.connect(host, data, port = 80, ssl = false, &callback) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/minhttp.rb', line 32

def self.connect(host, data, port=80, ssl=false, &callback)
  configure unless configured?

  EventMachine.connect(host, port, self) do |c|
    # this code runs after 'post_init', before 'connection_completed'
    c.host = host
    c.ssl = ssl
    c.callback = callback
    c.request_data = data
  end
end

.connectionsObject



17
18
19
# File 'lib/minhttp.rb', line 17

def self.connections
  @@connections
end

Instance Method Details

#connection_completedObject



60
61
62
63
64
65
66
67
68
# File 'lib/minhttp.rb', line 60

def connection_completed
  begin
    start_tls(:verify_peer => true) if @ssl
    send_data @request_data
  rescue Exception => e
    @@logger.error "Error in connection_completed: #{e}"
    raise e
  end
end

#post_initObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/minhttp.rb', line 44

def post_init
  begin
    @@connections += 1
    @parser = Http::Parser.new
    @headers_complete = false
    @parser.on_headers_complete = proc do
      @headers_complete = true
      :stop
    end
    @response_data = ""
  rescue Exception => e
    @@logger.error("Error in post_init: #{e}")
    raise e
  end
end

#receive_data(data) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/minhttp.rb', line 70

def receive_data(data)
  @response_data << data
  begin
    @parser << data unless @headers_complete
  rescue HTTP::Parser::Error => e
    @@logger.warn "Failed to parse: #{data}"
    raise e
  end
end

#ssl_handshake_completedObject

Verify the certs and throw an exception if they are not valid



108
109
110
111
112
113
114
115
116
# File 'lib/minhttp.rb', line 108

def ssl_handshake_completed
  begin
    return unless @@options[:verify_ssl]
    close_connection unless Http::SSLValidator.validate(@certs, @host)
  rescue Exception => e
    @@logger.error("Error in ssl_handshake_completed: #{e}")
    raise e
  end
end

#ssl_verify_peer(cert) ⇒ Object

Called once per cert received The certs aren’t verified until the handshake is completed



94
95
96
97
98
99
100
101
102
103
# File 'lib/minhttp.rb', line 94

def ssl_verify_peer(cert)
  begin
    @certs ||= []
    @certs << cert unless @certs.include?(cert)
    true
  rescue Exception => e
    @@logger.error("Error in ssl_verify_peer: #{e}")
    raise e
  end
end

#unbindObject



80
81
82
83
84
85
86
87
88
# File 'lib/minhttp.rb', line 80

def unbind
  begin
    @@connections -= 1
    @callback.call(@response_data, @parser)
  rescue Exception => e
    @@logger.error("Error in unbind: #{e}")
    raise e
  end
end