-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopher-server.rb
183 lines (131 loc) · 3.6 KB
/
gopher-server.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
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
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
180
181
182
183
#!/usr/bin/env ruby -w
#
# gopher-server [-p port] [root directory]
#
#
require 'socket'
require 'optparse'
TEXT = {
".c" => true,
".h" => true,
".txt" => true,
".text" => true,
".rb" => true,
'.mk' => true,
'.asm' => true,
'makefile' => true
}
def do_error(client, message)
client.write("i#{message}\r\n")
client.write(".\r\n")
end
def get_type(path)
path = path.downcase
ext = File.extname(path)
return 0 if TEXT[ext]
return 0 if TEXT[path]
return 9
end
def dd(client, name, path, type)
hostname = Thread.current[:hostname]
port = Thread.current[:port]
client.write("#{type}#{name}\t#{path}\t#{hostname}\t#{port}\r\n")
end
def do_directory(client, dir, root)
dir.each do |file|
next if file =~ /^\./
type = nil
path = dir.path + '/' + file
st = File::Stat.new(path)
if st.directory?
dd(client, file + '/', root + file + '/', '1')
next
end
if st.file?
type = get_type(file)
dd(client, file, root + file, type)
end
end
client.write(".\r\n")
end
def do_request(client)
req = client.gets("\r\n").chomp()
if req == nil || req == ""
do_directory(client, Dir.new('.'), '/')
return
end
# remove leading /
req.sub!(%r{^/*},'')
return do_error(client, 'Invalid request') if req =~ /\.\./
begin
st = File::Stat.new(req) or return do_error(client, 'Invalid Request')
rescue SystemCallError
return do_error(client, 'Invalid Request')
end
if st.directory?
# check for /../
req = '/' + req + '/'
req.gsub!(%r{//}, "/")
req.gsub!(%r{/./}, "/")
do_error(client, "Invalid resource") if req =~ /\.\./
do_directory(client, Dir.new('.' + req), req)
return
end
return unless st.file?
if get_type(req) == 0
# text
IO.foreach(req, "\n") {|x|
x.chomp!
x = "." + x if x =~ /^\./
client.write(x)
client.write("\r\n")
}
client.write(".\r\n")
else
# binary
client.binmode()
File.open(req, "rb") do |io|
loop do
x = io.read(256) or break
client.write(x)
end
end
end
end
# main
hostname = "imac.local"
port = 7070
OptionParser.new { |opts|
opts.banner = "Usage: gopher-server [-p port] [root-directory]"
opts.on('-p P', '--port P', Integer, 'Port') do |x|
port = x
puts "port = #{port}"
end
opts.on_tail('-h', '--help', 'Help') do
puts opts
exit
end
}.parse!
Dir.chdir(ARGV.pop()) if ARGV.length == 1
server = TCPServer.new("0.0.0.0", port)
puts("Listening on port #{port}")
loop do
Thread.start(server.accept) do |client|
begin
addr = client.getpeername() # client.remote_address() # client.peeraddr(false)
addr = addr.unpack("C*")
peer = addr[4,4].join('.')
Thread.current[:hostname] = hostname
Thread.current[:port] = port
puts("accept from #{peer}")
do_request(client)
client.flush()
sleep(1) # for marinetti
rescue Exception => error
puts("Error: #{error}")
print error.backtrace.join("\n")
end
client.close()
puts("connection closed")
end
end