-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.rb
More file actions
120 lines (96 loc) · 2.26 KB
/
pipe.rb
File metadata and controls
120 lines (96 loc) · 2.26 KB
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
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
#Test: in console, just curl localhost:3003/
#You can get the response
require 'socket'
require 'debugger'
require 'http/parser'
require 'stringio'
require 'thread'
class Pipe
def initialize(port, app)
@server = TCPServer.new(port)
@app = app
end
def prefork(workers)
puts "Master #{Process.pid}"
workers.times do
fork do
puts "Forked: #{Process.pid}"
start
end
end
Process.waitall
end
def start
loop do
socket = @server.accept
Thread.new do
connection = Connection.new(socket, @app)
connection.process
end
end
end
class Connection
def initialize(socket, app)
@socket = socket
@parser = Http::Parser.new(self)
@app = app
end
def process
until @socket.closed? || @socket.eof?
data = @socket.readpartial(1024)
@parser << data
end
end
def on_message_complete
puts "#{@parser.http_method} #{@parser.request_path}"
puts " " + "#{@parser.headers.inspect}"
env = {}
@parser.headers.each do |k, v|
env["HTTP_#{k.upcase.tr('-', '_')}"] = v
end
env["PATH_INFO"] = @parser.request_path
env["REQUEST_METHOD"] = @parser.http_method
env["rack.input"] = StringIO.new
send_response(env)
close
end
REASONS = {
200 => "OK",
404 => "Not found"
}
def send_response(env)
status, header, body = @app.call(env)
reason = REASONS[status]
response = "HTTP/1.1 #{status} #{REASONS[status]}\r\n"
header.each do |k, v|
response += "#{k} : #{v}\r\n"
end
response += "\r\n"
#The rack application should return a body respond to each method
body.each do |chunk|
response += "#{chunk}"
end
body.close if body.respond_to? :close
@socket.write(response)
end
def close
@socket.close
end
end
class Builder
attr_reader :app
def run(app)
@app = app
end
def self.parse_file(file)
content = File.read(file)
builder = self.new
builder.instance_eval(content)
builder.app
end
end
end
app = Pipe::Builder.parse_file("config.ru")
server = Pipe.new(3003, app)
p "You have a server for 3003"
server.prefork 3