-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_talk
executable file
·54 lines (42 loc) · 1.41 KB
/
run_talk
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
#!/usr/bin/env ruby
# Serve the current directory via HTTP.
# Like Python's SimpleHTTPServer, but with no-cache headers.
# Default port 8000, specify alternate port as first parameter:
# www 3000
# sudo www 80 # (probably a bad idea)
# Inspired by http://chrismdp.github.com/2011/12/cache-busting-ruby-http-server/
require "webrick"
require "shellwords"
class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler
DAY = 86400
def do_GET(request, response)
super
set_no_cache(response)
set_content_type(response)
end
private
def set_no_cache(response)
response["Content-Type"]
response["ETag"] = nil
response["Last-Modified"] = Time.now + DAY
response["Cache-Control"] = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
response["Pragma"] = "no-cache"
response["Expires"] = Time.now - DAY
end
def set_content_type(response)
response["Content-Type"] = content_type(response.filename)
end
def content_type(path)
case path.match(%r{\.(\w+)\z})[1]
when "html" then "text/html"
when "js" then "text/javascript"
when "css" then "text/css"
else `/usr/bin/file --brief --mime-type #{Shellwords.escape(path)}`.chomp
end
end
end
WEBrick::HTTPServer.new(Port: ARGV.first || 8000).tap do |server|
server.mount "/", NonCachingFileHandler , Dir.pwd
trap("INT") { server.stop }
server.start
end