-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtratamento.rb
146 lines (136 loc) · 3.15 KB
/
tratamento.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
require 'fileutils'
require 'base64'
require 'socket'
ERROS = {
'403' => 'Erro 403 Forbidden.',
'404' => 'Erro 404 Page Not Found. Sorry ):',
'400' => 'Erro 400 Bad Request.'
}
ACCPTS = {
'200' => '200 OK!',
'202' => '202 Accepted!'
}
# possiveis extensões
IMGTYPE = {
'image/png' => 'png',
'image/jpeg' => 'jpg'
}
@i = 1
@i2 = 1
def trataEntrada(url)
http = "http://"
path2 = '/'
#retira www.
if url =~ /[www.]/
url = url.split('www.')
url_correta = url.join
else
url_correta = url
end
#adiciona / pro path
if url_correta =~ /[\/]/
else
url_correta << path2
end
#adiciona http pro URI
if url_correta =~ /[http:]/
if url_correta[4] == "s"
puts "https não é aceito!"
exit!
end
else
url_correta = http << url_correta
end
return url_correta
end
def setPort(port)
if port.empty?
port = 80
return port
else
return port
end
end
def mostraContent(hostname)
Dir.chdir(hostname)
Dir.entries(Dir.pwd).each do |arquivo|
if arquivo == '.' || arquivo == '..'
next
elsif File.exist?(arquivo)
puts "#{arquivo} content:\n"
f = File.open(arquivo,"r")
puts f.read
f.close
end
end
puts "\nArquivos do diretorio: #{Dir.entries(Dir.pwd)}"
Dir.chdir("..")
end
def addFiles(hostname,path,port)
puts "Add arquivos a #{hostname}"
#HTTP request
socket = TCPSocket.new hostname, port
socket.puts("GET #{path} HTTP/1.1\r\nHost: #{hostname}\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent:BarbsClient/5.0 (X11; Linux x86_64)\r\nAccept: text/html\r\nAccept-Encoding: deflate\r\nAccept-Language: en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7\r\n\r\n")
response = socket.read
headers,body = response.split("\r\n\r\n", 2)
status = headers.split("\n").first
Dir.chdir(hostname)
if headers.match? /png|jpeg/
returnErros(status)
#nwrite imagefile in the index
filename = "imagem"
filename = filename << @i.to_s
filename = filename << ".jpg"
@i.to_i
@i += 1
f = File.new(filename, 'w')
f.write(body)
puts "Imagem adicionada!"
else
returnErros(status)
filename2 = "arquivo"
filename2 = filename2 << @i2.to_s
filename2 = filename2 << ".html"
@i2.to_i
@i2 += 1
f = File.new(filename2,"w")
f.write(body)
f.close
puts "Arquivo adicionado!"
end
Dir.chdir("..")
end
def escreveIndex(hostname,body,path)
Dir.mkdir(hostname)
puts "Diretorio criado."
Dir.chdir(hostname)
f = File.new("index.html","w")
f.write(body)
f.close
puts "Conteúdo da solicitação foi escrito!"
Dir.chdir("..")
end
def returnErros(statusCode)
method,number,word = statusCode.split(' ', 3)
if ERROS[number]
puts "#{ERROS[number]}"
exit!
elsif ACCPTS[number]
puts "All good. Status: #{ACCPTS[number]}"
end
end
def imageDecode(dirname,content,name)
# write file
Dir.mkdir(dirname)
puts "Diretorio criado."
Dir.chdir(dirname)
#name the file
filename = "imagem"
filename = filename << @i.to_s
filename = filename << ".jpg"
@i.to_i
@i += 1
f = File.new(filename, 'w')
f.write(content)
Dir.chdir('..')
end