Class: Async::HTTP::Protocol::HTTP1::Server

Inherits:
Connection
  • Object
show all
Defined in:
lib/async/http/protocol/http1/server.rb

Overview

An HTTP/1 server connection that receives requests and sends responses.

Instance Attribute Summary

Attributes inherited from Connection

#count, #version

Instance Method Summary collapse

Methods inherited from Connection

#as_json, #concurrency, #http1?, #http2?, #peer, #reusable?, #to_json, #to_s, #viable?

Constructor Details

#initializeServer

Initialize the HTTP/1 server connection.



22
23
24
25
26
# File 'lib/async/http/protocol/http1/server.rb', line 22

def initialize(...)
	super
	
	@ready = Async::Notification.new
end

Instance Method Details

#closed(error = nil) ⇒ Object

Called when the connection is closed, signalling any waiting tasks.



29
30
31
32
33
# File 'lib/async/http/protocol/http1/server.rb', line 29

def closed(error = nil)
	super
	
	@ready.signal
end

#each(task: Task.current) ⇒ Object

Server loop.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/async/http/protocol/http1/server.rb', line 70

def each(task: Task.current)
	task.annotate("Reading #{self.version} requests for #{self.class}.")
	
	while request = next_request
		# We have received complete request (line + headers), so defer stop until the response is generated.
		task.defer_stop do
			if body = request.body
				finishable = Finishable.new(body)
				request.body = finishable
			end
			
			response = yield(request, self)
			version = request.version
			body = response&.body
			
			if hijacked?
				body&.close
				return
			end
			
			# If a response was generated, send it:
			if response
				trailer = response.headers.trailer!
				headers = response.headers.header
				
				# Some operations in this method are long running, that is, it's expected that `body.call(stream)` could literally run indefinitely. In order to facilitate garbage collection, we want to nullify as many local variables before calling the streaming body. This ensures that the garbage collection can clean up as much state as possible during the long running operation, so we don't retain objects that are no longer needed.
				
				if body and protocol = response.protocol
					# We force a 101 response if the protocol is upgraded - HTTP/2 CONNECT will return 200 for success, but this won't be understood by HTTP/1 clients:
					write_response(@version, 101, headers)
					
					# At this point, the request body is hijacked, so we don't want to call #finish below.
					request = nil
					response = nil
					
					if body.stream?
						return body.call(write_upgrade_body(protocol))
					else
						write_upgrade_body(protocol, body)
					end
				elsif response.status == 101
					# This code path is to support legacy behavior where the response status is set to 101, but the protocol is not upgraded. This may not be a valid use case, but it is supported for compatibility. We expect the response headers to contain the `upgrade` header.
					write_response(@version, response.status, headers)
					
					# Same as above:
					request = nil
					response = nil
					
					if body.stream?
						return body.call(write_tunnel_body(version))
					else
						write_tunnel_body(version, body)
					end
				else
					write_response(@version, response.status, headers)
					
					if request.connect? and response.success?
						# Same as above:
						request = nil
						response = nil
						
						if body.stream?
							return body.call(write_tunnel_body(version))
						else
							write_tunnel_body(version, body)
						end
					else
						head = request.head?
						
						# Same as above:
						request = nil
						response = nil
						
						write_body(version, body, head, trailer)
					end
				end
				
				# We are done with the body:
				body = nil
			else
				# If the request failed to generate a response, it was an internal server error:
				write_response(@version, 500, {})
				write_body(version, nil)
				
				request&.finish
			end
			
			if finishable
				finishable.wait(@persistent)
			else
				# Do not remove this line or you will unleash the gods of concurrency hell.
				task.yield
			end
		rescue => error
			# We store error (as a local variable) for later use in the ensure block.
			raise
		ensure
			body&.close(error)
		end
	end
end

#fail_request(status) ⇒ Object

Write a failure response with the given status code.



37
38
39
40
41
42
43
44
# File 'lib/async/http/protocol/http1/server.rb', line 37

def fail_request(status)
	@persistent = false
	write_response(@version, status, {})
	write_body(@version, nil)
rescue => error
	# At this point, there is very little we can do to recover:
	Console.debug(self, "Failed to write failure response!", error)
end

#next_requestObject

Read the next incoming request from the connection.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/async/http/protocol/http1/server.rb', line 48

def next_request
	if closed?
		return nil
	elsif !idle?
		@ready.wait
	end
	
	# Read an incoming request:
	return unless request = Request.read(self)
	
	unless persistent?(request.version, request.method, request.headers)
		@persistent = false
	end
	
	return request
rescue ::Protocol::HTTP1::BadRequest
	fail_request(400)
	# Conceivably we could retry here, but we don't really know how bad the error is, so it's better to just fail:
	raise
end