-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastcgi_executor.rb
53 lines (44 loc) · 1.34 KB
/
fastcgi_executor.rb
1
2
3
4
5
6
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'socket'
load 'fastcgi.rb'
class FastcgiExecutor
def initialize(socket, dispatcher)
@fastcgi_socket = FastCGISocket.new socket
@dispatcher = dispatcher
@random = Random.new
end
def cleanup()
@fastcgi_socket.close()
end
def execute(event_data)
request_id = @random.rand(10000)
beg = BeginRequestRecord.new request_id, FCGI_RESPONDER, 0
params = ParamsRecord.new request_id, {
'SCRIPT_FILENAME' => @dispatcher,
'REQUEST_METHOD' => 'POST',
'DEFERRED_DATA' => event_data
}
stdin = StdinDataRecord.new request_id, ''
@fastcgi_socket.send_record beg
@fastcgi_socket.send_record params
@fastcgi_socket.send_record stdin
stdout_buf = ''
stderr_buf = ''
loop do
rec = @fastcgi_socket.read_record
case rec.type
when FCGI_END_REQUEST
if rec.application_status != 0
raise StandardError, stderr_buf
end
break
when FCGI_STDOUT
stdout_buf << rec.flagment
when FCGI_STDERR
stderr_buf << rec.flagment
else
raise "got unknown record: #{rec.class}"
end
end
# puts stdout_buf
end
end