-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathconfig.ru
67 lines (58 loc) · 1.43 KB
/
config.ru
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
require "rack"
require "rack/static"
require "net/http"
require "uri"
@root = File.dirname(__FILE__)
DIRECTORIES = %w{/css /images /js /lib /media}.freeze
PAGES = %w{/index.html}.freeze
use Rack::Static, :urls => DIRECTORIES, :root => "#{@root}/public"
def relay_audio(request)
audio_url = request.params["src"]
uri = URI.parse(audio_url)
http = Net::HTTP.new(uri.host, uri.port)
if audio_url =~ /^https/
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new(uri.request_uri)
http.request(request)
end
def not_found
[404, {"Content-Type" => "text/html"}, ["not found"]]
end
def render_audio(env)
request = Rack::Request.new(env)
if request.params["src"].nil?
not_found
else
response = relay_audio(request)
if response.code == "200" && (type = response["content-type"]) =~ /^audio\//
[200, {"Content-Type" => type}, [response.body]]
else
not_found
end
end
end
def render_page(path)
file = path.scan(/\/(\w+)\.html/)[0][0]
[
200,
{
"Content-Type" => "text/html",
"Cache-Control" => "public, max-age=1"
},
File.open("#{@root}/public/#{file}.html", File::RDONLY)
]
end
app = Proc.new do |env|
path = Rack::Utils.unescape(env["PATH_INFO"])
path = PAGES.first if path == "/"
if path == "/audio"
render_audio(env)
elsif PAGES.include?(path)
render_page(path)
else
not_found
end
end
run(app)