-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_server.rb
42 lines (37 loc) · 1.01 KB
/
web_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
require 'socket'
class WebServer
attr_accessor :host, :port
def initialize(app, attrs={})
@app = app
@host = attrs.fetch(:host, 'localhost')
@port = attrs.fetch(:port, 2345)
@server = TCPServer.new(@host, @port)
end
def run
begin
puts "You start webserver on http://#{@host}:#{@port}. Press ctrl-C for exit."
loop do
socket = @server.accept
request = socket.gets
# TODO: Any query performed as executable code.
# NEED: Detect non executable files.
# NEED: Detect /../../password paths and remove it.
socket.print(perform_app({request: request}))
socket.close
end
rescue Interrupt => e
puts "\nServer was stopped."
end
end
def perform_app(env={})
app_response = @app.call(env)
res= [
"HTTP/1.1 #{app_response[0]}",
"Content-Type: #{app_response[1]}",
"Content-Length: #{app_response[2].bytesize}",
"Connection: close",
"",
app_response[2]
].join("\r\n")
end
end