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.



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

def callback
  @callback
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#request_dataObject

Returns the value of attribute request_data.



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

def request_data
  @request_data
end

#sslObject

Returns the value of attribute ssl.



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

def ssl
  @ssl
end

Class Method Details

.configure(options = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/minhttp.rb', line 23

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

.configured?Boolean

Returns:

  • (Boolean)


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

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

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/minhttp.rb', line 35

def self.connect(host, data, port=80, ssl=false, &callback)
  configure unless configured?
  unless (data =~ /\A[^\r\n]+HTTP\/1\.0/ || data =~ /Connection[^\r\n]+close/)
    raise MinHttp::PersistentConnectionException.new("MinHTTP does not support persistent connections, so the request data must indicate HTTP 1.0 or it must include a 'Connection: close' header")
  end

  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



19
20
21
# File 'lib/minhttp.rb', line 19

def self.connections
  @@connections
end

Instance Method Details

#connection_completedObject



66
67
68
69
70
71
72
73
74
# File 'lib/minhttp.rb', line 66

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/minhttp.rb', line 50

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



76
77
78
79
80
81
82
83
84
# File 'lib/minhttp.rb', line 76

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



122
123
124
125
126
127
128
129
130
# File 'lib/minhttp.rb', line 122

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

#ssl_verification_enabled?(host) ⇒ Boolean

SSL verification is only on if the ‘verify_ssl’ option is set and if the host is not on the whitelist

Returns:

  • (Boolean)


136
137
138
139
140
141
# File 'lib/minhttp.rb', line 136

def ssl_verification_enabled?(host)
  return false unless @@options[:verify_ssl]
  # the ssl_whitelist is an array of regular expressions
  return false if @@options[:ssl_whitelist].find { |r| host =~ r }
  return true
end

#ssl_verify_peer(cert) ⇒ Object

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



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

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/minhttp.rb', line 86

def unbind
  begin
    @@connections -= 1

    # combine the chunks of a chunked response
    if @parser.headers["Transfer-Encoding"] == "chunked"
      headers, body = @response_data.split("\r\n\r\n", 2)
      body = MinHttp::Chunk.new(body).parse
      @response_data = "#{headers}\r\n\r\n#{body}"
    end

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