Class: Async::HTTP::Server
- Inherits:
-
Protocol::HTTP::Middleware
- Object
- Protocol::HTTP::Middleware
- Async::HTTP::Server
- Defined in:
- lib/async/http/server.rb
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
Class Method Summary collapse
Instance Method Summary collapse
- #accept(peer, address, task: Task.current) ⇒ Object
- #as_json ⇒ Object
-
#initialize(app, endpoint, protocol: endpoint.protocol, scheme: endpoint.scheme) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(app, endpoint, protocol: endpoint.protocol, scheme: endpoint.scheme) ⇒ Server
Returns a new instance of Server.
21 22 23 24 25 26 27 |
# File 'lib/async/http/server.rb', line 21 def initialize(app, endpoint, protocol: endpoint.protocol, scheme: endpoint.scheme) super(app) @endpoint = endpoint @protocol = protocol @scheme = scheme end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
41 42 43 |
# File 'lib/async/http/server.rb', line 41 def endpoint @endpoint end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
42 43 44 |
# File 'lib/async/http/server.rb', line 42 def protocol @protocol end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
43 44 45 |
# File 'lib/async/http/server.rb', line 43 def scheme @scheme end |
Class Method Details
.for(*arguments, **options, &block) ⇒ Object
17 18 19 |
# File 'lib/async/http/server.rb', line 17 def self.for(*arguments, **, &block) self.new(block, *arguments, **) end |
Instance Method Details
#accept(peer, address, task: Task.current) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/async/http/server.rb', line 45 def accept(peer, address, task: Task.current) connection = @protocol.server(peer) Console.logger.debug(self) {"Incoming connnection from #{address.inspect} to #{@protocol}"} connection.each do |request| # We set the default scheme unless it was otherwise specified. # https://tools.ietf.org/html/rfc7230#section-5.5 request.scheme ||= self.scheme # Console.logger.debug(self) {"Incoming request from #{address.inspect}: #{request.method} #{request.path}"} # If this returns nil, we assume that the connection has been hijacked. self.call(request) end ensure connection&.close end |
#as_json ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/async/http/server.rb', line 29 def as_json(...) { endpoint: @endpoint.to_s, protocol: @protocol, scheme: @scheme, } end |
#run ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/async/http/server.rb', line 65 def run Async do |task| @endpoint.accept(&self.method(:accept)) # Wait for all children to finish: task.children.each(&:wait) end end |
#to_json ⇒ Object
37 38 39 |
# File 'lib/async/http/server.rb', line 37 def to_json(...) as_json.to_json(...) end |