Class: Puma::Binder
- Inherits:
-
Object
show all
- Includes:
- Const
- Defined in:
- lib/puma/binder.rb
Constant Summary
Constants included
from Const
Const::CGI_VER, Const::CHUNK_SIZE, Const::CLOSE, Const::CLOSE_CHUNKED, Const::CODE_NAME, Const::COLON, Const::CONNECTION_CLOSE, Const::CONNECTION_KEEP_ALIVE, Const::CONTENT_LENGTH, Const::CONTENT_LENGTH2, Const::CONTENT_LENGTH_S, Const::CONTENT_TYPE, Const::DATE, Const::ERROR_400_RESPONSE, Const::ERROR_404_RESPONSE, Const::ERROR_500_RESPONSE, Const::ERROR_503_RESPONSE, Const::ETAG, Const::ETAG_FORMAT, Const::FAST_TRACK_KA_TIMEOUT, Const::FIRST_DATA_TIMEOUT, Const::GATEWAY_INTERFACE, Const::GET, Const::HALT_COMMAND, Const::HEAD, Const::HIJACK, Const::HIJACK_IO, Const::HIJACK_P, Const::HOST, Const::HTTP, Const::HTTPS, Const::HTTPS_KEY, Const::HTTP_10, Const::HTTP_10_200, Const::HTTP_11, Const::HTTP_11_200, Const::HTTP_CONNECTION, Const::HTTP_HOST, Const::HTTP_IF_MODIFIED_SINCE, Const::HTTP_IF_NONE_MATCH, Const::HTTP_VERSION, Const::HTTP_X_FORWARDED_FOR, Const::KEEP_ALIVE, Const::LAST_MODIFIED, Const::LINE_END, Const::LOCALHOST, Const::MAX_BODY, Const::MAX_HEADER, Const::NEWLINE, Const::PATH_INFO, Const::PERSISTENT_TIMEOUT, Const::PORT_443, Const::PORT_80, Const::PUMA_CONFIG, Const::PUMA_SOCKET, Const::PUMA_TMP_BASE, Const::PUMA_VERSION, Const::RACK_AFTER_REPLY, Const::RACK_INPUT, Const::RACK_URL_SCHEME, Const::REDIRECT, Const::REMOTE_ADDR, Const::REQUEST_METHOD, Const::REQUEST_PATH, Const::REQUEST_URI, Const::RESTART_COMMAND, Const::SCRIPT_NAME, Const::SERVER_NAME, Const::SERVER_PORT, Const::SERVER_PROTOCOL, Const::SERVER_SOFTWARE, Const::SLASH, Const::STATUS_FORMAT, Const::STOP_COMMAND, Const::TRANSFER_ENCODING, Const::TRANSFER_ENCODING_CHUNKED
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add_ssl_listener(host, port, ctx, optimize_for_latency = true, backlog = 1024) ⇒ Object
-
#add_tcp_listener(host, port, optimize_for_latency = true, backlog = 1024) ⇒ Object
Tell the server to listen on host host
, port port
.
-
#add_unix_listener(path, umask = nil) ⇒ Object
Tell the server to listen on path
as a UNIX domain socket.
-
#close ⇒ Object
-
#env(sock) ⇒ Object
-
#import_from_env ⇒ Object
-
#inherit_tcp_listener(host, port, fd) ⇒ Object
-
#inherit_unix_listener(path, fd) ⇒ Object
-
#inherited_ssl_listener(fd, ctx) ⇒ Object
-
#initialize(events) ⇒ Binder
constructor
A new instance of Binder.
-
#parse(binds, logger) ⇒ Object
Constructor Details
#initialize(events) ⇒ Binder
Returns a new instance of Binder.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/puma/binder.rb', line 7
def initialize(events)
@events = events
@listeners = []
@inherited_fds = {}
@unix_paths = []
@proto_env = {
"rack.version".freeze => Rack::VERSION,
"rack.errors".freeze => events.stderr,
"rack.multithread".freeze => true,
"rack.multiprocess".freeze => false,
"rack.run_once".freeze => false,
"SCRIPT_NAME".freeze => ENV['SCRIPT_NAME'] || "",
"CONTENT_TYPE".freeze => "text/plain",
"QUERY_STRING".freeze => "",
SERVER_PROTOCOL => HTTP_11,
SERVER_SOFTWARE => PUMA_VERSION,
GATEWAY_INTERFACE => CGI_VER
}
@envs = {}
@ios = []
end
|
Instance Attribute Details
#ios ⇒ Object
Returns the value of attribute ios.
36
37
38
|
# File 'lib/puma/binder.rb', line 36
def ios
@ios
end
|
#listeners ⇒ Object
Returns the value of attribute listeners.
36
37
38
|
# File 'lib/puma/binder.rb', line 36
def listeners
@listeners
end
|
Instance Method Details
#add_ssl_listener(host, port, ctx, optimize_for_latency = true, backlog = 1024) ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/puma/binder.rb', line 211
def add_ssl_listener(host, port, ctx,
optimize_for_latency=true, backlog=1024)
if IS_JRUBY
@events.error "SSL not supported on JRuby"
raise UnsupportedOption
end
require 'puma/minissl'
s = TCPServer.new(host, port)
if optimize_for_latency
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
end
s.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
s.listen backlog
ssl = MiniSSL::Server.new s, ctx
env = @proto_env.dup
env[HTTPS_KEY] = HTTPS
@envs[ssl] = env
@ios << ssl
s
end
|
#add_tcp_listener(host, port, optimize_for_latency = true, backlog = 1024) ⇒ Object
Tell the server to listen on host host
, port port
. If optimize_for_latency
is true (the default) then clients connecting will be optimized for latency over throughput.
backlog
indicates how many unaccepted connections the kernel should allow to accumulate before returning connection refused.
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/puma/binder.rb', line 188
def add_tcp_listener(host, port, optimize_for_latency=true, backlog=1024)
host = host[1..-2] if host[0..0] == '['
s = TCPServer.new(host, port)
if optimize_for_latency
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
end
s.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
s.listen backlog
@ios << s
s
end
|
#add_unix_listener(path, umask = nil) ⇒ Object
Tell the server to listen on path
as a UNIX domain socket.
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/puma/binder.rb', line 250
def add_unix_listener(path, umask=nil)
@unix_paths << path
umask ||= 0
begin
old_mask = File.umask(umask)
if File.exists? path
begin
old = UNIXSocket.new path
rescue SystemCallError
File.unlink path
else
old.close
raise "There is already a server bound to: #{path}"
end
end
s = UNIXServer.new(path)
@ios << s
ensure
File.umask old_mask
end
s
end
|
#close ⇒ Object
42
43
44
45
|
# File 'lib/puma/binder.rb', line 42
def close
@ios.each { |i| i.close }
@unix_paths.each { |i| File.unlink i }
end
|
#env(sock) ⇒ Object
38
39
40
|
# File 'lib/puma/binder.rb', line 38
def env(sock)
@envs.fetch(sock, @proto_env)
end
|
#import_from_env ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/puma/binder.rb', line 47
def import_from_env
remove = []
ENV.each do |k,v|
if k =~ /PUMA_INHERIT_\d+/
fd, url = v.split(":", 2)
@inherited_fds[url] = fd.to_i
remove << k
end
if k =~ /LISTEN_FDS/ && ENV['LISTEN_PID'].to_i == $$
v.to_i.times do |num|
fd = num + 3
sock = TCPServer.for_fd(fd)
begin
url = "unix://" + Socket.unpack_sockaddr_un(sock.getsockname)
rescue ArgumentError
port, addr = Socket.unpack_sockaddr_in(sock.getsockname)
if addr =~ /\:/
addr = "[#{addr}]"
end
url = "tcp://#{addr}:#{port}"
end
@inherited_fds[url] = sock
end
ENV.delete k
ENV.delete 'LISTEN_PID'
end
end
remove.each do |k|
ENV.delete k
end
end
|
#inherit_tcp_listener(host, port, fd) ⇒ Object
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/puma/binder.rb', line 200
def inherit_tcp_listener(host, port, fd)
if fd.kind_of? TCPServer
s = fd
else
s = TCPServer.for_fd(fd)
end
@ios << s
s
end
|
#inherit_unix_listener(path, fd) ⇒ Object
279
280
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/puma/binder.rb', line 279
def inherit_unix_listener(path, fd)
@unix_paths << path
if fd.kind_of? TCPServer
s = fd
else
s = UNIXServer.for_fd fd
end
@ios << s
s
end
|
#inherited_ssl_listener(fd, ctx) ⇒ Object
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/puma/binder.rb', line 236
def inherited_ssl_listener(fd, ctx)
if IS_JRUBY
@events.error "SSL not supported on JRuby"
raise UnsupportedOption
end
require 'puma/minissl'
s = TCPServer.for_fd(fd)
@ios << MiniSSL::Server.new(s, ctx)
s
end
|
#parse(binds, logger) ⇒ Object
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
171
172
173
174
175
176
177
178
179
|
# File 'lib/puma/binder.rb', line 81
def parse(binds, logger)
binds.each do |str|
uri = URI.parse str
case uri.scheme
when "tcp"
if fd = @inherited_fds.delete(str)
logger.log "* Inherited #{str}"
io = inherit_tcp_listener uri.host, uri.port, fd
else
logger.log "* Listening on #{str}"
io = add_tcp_listener uri.host, uri.port
end
@listeners << [str, io]
when "unix"
path = "#{uri.host}#{uri.path}"
if fd = @inherited_fds.delete(str)
logger.log "* Inherited #{str}"
io = inherit_unix_listener path, fd
else
logger.log "* Listening on #{str}"
umask = nil
if uri.query
params = Rack::Utils.parse_query uri.query
if u = params['umask']
umask = Integer(u)
end
end
io = add_unix_listener path, umask
end
@listeners << [str, io]
when "ssl"
if IS_JRUBY
@events.error "SSL not supported on JRuby"
raise UnsupportedOption
end
params = Rack::Utils.parse_query uri.query
require 'puma/minissl'
ctx = MiniSSL::Context.new
unless params['key']
@events.error "Please specify the SSL key via 'key='"
end
ctx.key = params['key']
unless params['cert']
@events.error "Please specify the SSL cert via 'cert='"
end
ctx.cert = params['cert']
ctx.verify_mode = MiniSSL::VERIFY_NONE
if fd = @inherited_fds.delete(str)
logger.log "* Inherited #{str}"
io = inherited_ssl_listener fd, ctx
else
logger.log "* Listening on #{str}"
io = add_ssl_listener uri.host, uri.port, ctx
end
@listeners << [str, io]
else
logger.error "Invalid URI: #{str}"
end
end
@inherited_fds.each do |str, fd|
logger.log "* Closing unused inherited connection: #{str}"
begin
if fd.kind_of? TCPServer
fd.close
else
IO.for_fd(fd).close
end
rescue SystemCallError
end
uri = URI.parse str
if uri.scheme == "unix"
path = "#{uri.host}#{uri.path}"
File.unlink path
end
end
end
|