Class: Lifter::Connection

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

Defined Under Namespace

Classes: Request

Constant Summary collapse

PAYLOAD_METHODS =
['put', 'post'].freeze
CONTENT_TYPE_KEY =
'content-type'.freeze
MULTIPART_CONTENT_TYPE =
'multipart/form-data'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lifter/connection.rb', line 14

def initialize
  super

  @is_multipart = nil
  @has_payload = nil

  @multipart_reader = nil
  @inline_reader = nil

  @server = nil
  @request = Request.new

  @parser = HTTP::Parser.new

  @parser.on_message_begin = proc do
    start_request
  end

  @parser.on_headers_complete = proc do
    process_headers
    start_payload if payload?
  end

  @parser.on_body = proc do |data|
    receive_payload_data(data) if payload?
  end

  @parser.on_message_complete = proc do
    finish_request
  end
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



12
13
14
# File 'lib/lifter/connection.rb', line 12

def request
  @request
end

Instance Method Details

#closeObject



81
82
83
84
85
# File 'lib/lifter/connection.rb', line 81

def close
  EventMachine.next_tick do
    close_connection(true)
  end
end

#file_managerObject

Raises:

  • (StandardError)


52
53
54
55
56
# File 'lib/lifter/connection.rb', line 52

def file_manager
  raise StandardError.new('server not defined') if @server.nil?

  @server.file_manager
end

#http_versionObject



66
67
68
# File 'lib/lifter/connection.rb', line 66

def http_version
  @parser.http_version || [1, 0]
end

#receive_data(data) ⇒ Object



58
59
60
# File 'lib/lifter/connection.rb', line 58

def receive_data(data)
  @parser << data
end

#remote_ipObject



70
71
72
# File 'lib/lifter/connection.rb', line 70

def remote_ip
  '127.0.0.1'
end

#respond(code, status) ⇒ Object



74
75
76
77
78
79
# File 'lib/lifter/connection.rb', line 74

def respond(code, status)
  EventMachine.next_tick do
    response = "HTTP/#{http_version.join('.')} #{code} #{status}"
    send_data(response)
  end
end

#server=(server) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
# File 'lib/lifter/connection.rb', line 46

def server=(server)
  raise ArgumentError.new('incorrect type') if !server.is_a?(Server)

  @server = server
end

#unbindObject



62
63
64
# File 'lib/lifter/connection.rb', line 62

def unbind
  @payload.cancel if !@payload.nil?
end