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
58
59
60
61
62
63
64
65
66
67
68
69
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
|
# File 'lib/control_tower/rack_socket.rb', line 31
def open
@status = :open
while (@status == :open)
connection = @socket.accept
@request_queue.async(@request_group) do
env = { 'rack.errors' => $stderr,
'rack.multiprocess' => false,
'rack.multithread' => @multithread,
'rack.run_once' => false,
'rack.version' => VERSION }
resp = nil
= 'X-Sendfile'
x_sendfile = nil
begin
request_data = parse!(connection, env)
if request_data
request_data['REMOTE_ADDR'] = connection.addr[3]
status, , body = @app.call(request_data)
if .has_key?()
x_sendfile = []
x_sendfile = ::File.open(x_sendfile, 'r') unless x_sendfile.kind_of? IO
x_sendfile_size = x_sendfile.stat.size
.delete()
['Content-Length'] = x_sendfile_size
end
unless (status == -1 ||
(status >= 100 and status <= 199) ||
status == 204 ||
status == 304 ||
.has_key?('Content-Length'))
['Content-Length'] = if body.respond_to?(:each)
size = 0
body.each { |x| size += x.bytesize }
size
else
body.bytesize
end
end
['Connection'] = 'close'
resp = "HTTP/1.1 #{status}\r\n"
.each do |, value|
resp << "#{}: #{value}\r\n"
end
resp << "\r\n"
connection.write resp
if x_sendfile
connection.sendfile(x_sendfile, 0, x_sendfile_size)
elsif body.respond_to?(:each)
body.each do |chunk|
connection.write chunk
end
else
connection.write body
end
else
$stderr.puts "Error: No request data received!"
end
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, Errno::EINVAL
$stderr.puts "Error: Connection terminated!"
rescue Object => e
if resp.nil? && !connection.closed?
connection.write "HTTP/1.1 400\r\n\r\n"
else
$stderr.puts "Error: Problem transmitting data -- #{e.inspect}"
$stderr.puts e.backtrace.join("\n")
end
ensure
input = env['rack.input']
input.unlink if input.class == Tempfile
connection.close rescue nil
end
end
end
end
|