Class: Lifter::Connection
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- Lifter::Connection
- Defined in:
- lib/lifter/connection.rb
Defined Under Namespace
Classes: Request
Constant Summary collapse
- PAYLOAD_METHODS =
['put', 'post'].freeze
- OPTIONS_METHOD =
'options'.freeze
- CONTENT_TYPE_KEY =
'content-type'.freeze
- MULTIPART_CONTENT_TYPE =
'multipart/form-data'.freeze
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
- #cancel ⇒ Object
- #close(opts = {}) ⇒ Object
- #file_manager ⇒ Object
- #gracefully_close? ⇒ Boolean
- #http_version ⇒ Object
-
#initialize ⇒ Connection
constructor
A new instance of Connection.
- #receive_data(data) ⇒ Object
- #remote_ip ⇒ Object
- #respond(code, status, headers = {}, body = '') ⇒ Object
- #server=(server) ⇒ Object
- #unbind ⇒ Object
Constructor Details
#initialize ⇒ Connection
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 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lifter/connection.rb', line 15 def initialize super @is_multipart = nil @has_payload = nil @options_request = nil @multipart_reader = nil @inline_reader = nil @server = nil @request = Request.new @gracefully_close = false @parser = HTTP::Parser.new @parser. = proc do start_request end @parser.on_headers_complete = proc do process_headers if payload? start_payload else if request_method?(:options) else close(gracefully: true) end end end @parser.on_body = proc do |data| receive_payload_data(data) if payload? end @parser. = proc do finish_request end end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
13 14 15 |
# File 'lib/lifter/connection.rb', line 13 def request @request end |
Instance Method Details
#cancel ⇒ Object
87 88 89 90 |
# File 'lib/lifter/connection.rb', line 87 def cancel @payload.cancel if !@payload.nil? close(gracefully: true) end |
#close(opts = {}) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/lifter/connection.rb', line 79 def close(opts = {}) @gracefully_close = opts[:gracefully] == true EventMachine.next_tick do close_connection(true) end end |
#file_manager ⇒ Object
65 66 67 68 69 |
# File 'lib/lifter/connection.rb', line 65 def file_manager raise StandardError.new('server not defined') if @server.nil? @server.file_manager end |
#gracefully_close? ⇒ Boolean
92 93 94 |
# File 'lib/lifter/connection.rb', line 92 def gracefully_close? @gracefully_close == true end |
#http_version ⇒ Object
96 97 98 |
# File 'lib/lifter/connection.rb', line 96 def http_version @parser.http_version || [1, 0] end |
#receive_data(data) ⇒ Object
71 72 73 |
# File 'lib/lifter/connection.rb', line 71 def receive_data(data) @parser << data end |
#remote_ip ⇒ Object
100 101 102 |
# File 'lib/lifter/connection.rb', line 100 def remote_ip '127.0.0.1' end |
#respond(code, status, headers = {}, body = '') ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/lifter/connection.rb', line 104 def respond(code, status, headers = {}, body = '') return if gracefully_close? EventMachine.next_tick do response = "HTTP/#{http_version.join('.')} #{code} #{status}" headers = {} if headers.empty? headers[:content_length] = body.bytesize if headers[:content_length].nil? headers[:access_control_allow_origin] = @request.origin if !@request.origin.nil? formatted_headers = format_response_headers(headers) response << "\r\n" << formatted_headers response << "\r\n\r\n" response << body send_data(response) end end |
#server=(server) ⇒ Object
59 60 61 62 63 |
# File 'lib/lifter/connection.rb', line 59 def server=(server) raise ArgumentError.new('incorrect type') if !server.is_a?(Server) @server = server end |
#unbind ⇒ Object
75 76 77 |
# File 'lib/lifter/connection.rb', line 75 def unbind @payload.cancel if !gracefully_close? && !@payload.nil? end |