-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhttp.rb
169 lines (152 loc) · 4.01 KB
/
rhttp.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
# rhttp - Simple Web Server.
# Simple servidor http programado en Ruby.
# Tal vez haga mi tesis con esto :D
require 'socket'
class RHTTPServer
def initialize(rootdir, port)
@rootdir = rootdir
@port = port
end
def start
serversocket = TCPServer.open(@port)
loop do
clientsocket = serversocket.accept
Thread.new do
puts "\r\nClient connected :)"
puts "Launching worker for him..."
RHTTPWorker.new(@rootdir, clientsocket).start
# Worker despacha la petición
end
end
serversocket.close
end
end
class RHTTPWorker
def initialize(rootdir, socket)
@rootdir = rootdir
@socket = socket
end
def start
begin
# Obteniendo petición
request = @socket.gets
#puts request
puts "INCOMING REQUEST"
# Descomponiendo petición
if request != nil
reqtype, reqresource, reqprotocol = request.split(" ")
# Identificando query string
reqresource, querystring = reqresource.split("?")
puts "Request type is "+reqtype
puts "Requested resource is "+reqresource
end
# Tipo de petición
case reqtype
when "GET"
# Buscando el archivo solicitado
# Cambiando el nombre del recurso raíz
if reqresource == "/"
reqresource = "/index.php"
if File.exist?(@rootdir+"/index.html")
reqresource = "/index.html"
end
end
@html = "" # Reiniciando @html
@response = "" # Reiniciando @response
# Abriendo el recurso
if File.exist?(@rootdir+reqresource)
# Identificando el Content-Type por extensión del
# recurso.
# TODO: Mejor método. Este método es poco fiable.
is_php = false
ext = reqresource.split(".").last
case ext
when "jpg"
ctype = "image/jpeg"
when "png"
ctype = "image/png"
when "gif"
ctype = "image/gif"
when "js"
ctype = "text/javascript"
when "css"
ctype = "text/css"
when "html"
ctype = "text/html"
when "php"
# ctype es determinado por lo que devuelva php-cgi
is_php = true
end
if is_php
# Preprocesando el PHP para enviar la salida como HTML
puts "Preprocessing PHP resource..."
# Preparando el entorno para ejecutar php-cgi
ENV['REDIRECT_STATUS'] = "200" # Necesaria para ejecutar php-cgi
ENV['SCRIPT_FILENAME'] = @rootdir+reqresource
ENV['QUERY_STRING'] = querystring
ENV['REQUEST_METHOD'] = reqtype
# Lanzando php-cgi
output = ""
IO.popen("php-cgi") do |pipe|
while line = pipe.gets
if line.start_with?("Content-type: ")
ctype = line
end
output += line
end
end
ctype = ctype.split(": ").last.chomp
@html = output.split(ctype).last
else
# Leyendo el contenido sin preprocesar
res = File.open(@rootdir+reqresource, "r") do |file|
while line = file.gets
@html += line
end
end
puts "Loaded content of resource"
end
size = (@html.size*8).to_s
@response = "HTTP/1.1 200 OK\r\n"
@response += "Server: rhttp 0.1 (Linux Mint)\r\n"
@response += "Content-Type: #{ctype}\r\n"
@response += "Content-Length: #{size}\r\n"
else
puts "Resource not found"
@response = "HTTP/1.1 404 NOT FOUND\r\n"
@response += "Server: rhttp 0.1 (Linux Mint)\r\n"
@html = "<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>Sorry :(</h1>
The requested resource '#{reqresource}' was not found.<br/>
Check for typos.<br/>
<hr/>
<h6>rhttp 1.0 (Linux Mint)</h6>
</body>
</html>"
end
end
# Enviando datos
@socket.puts(@response+"\r\n"+@html)
puts "Response sent"
puts "Worker. Change and Out. *TKKK*"
@socket.close
rescue Exception => e
puts "Exception: "
puts e.message
puts e.backtrace.inspect
ensure
@socket.close
end
end
end
# Punto de entrada
#puts "Bienvenido a rhttp ;D"
print "Número de puerto: "
port = gets.chomp
#puts "Iniciando servidor..."
server = RHTTPServer.new("www", port)
server.start