4 ).each &method(:require)
6 Encoding.default_external = Encoding::BINARY
9 class R < Watts::Resource
10 # This is a stupid feature.
11 # Fortunes lifted shamelessly from http://quotes.cat-v.org/programming/
12 Fortunes = <<-EOFORTUNES.gsub(/\t+/, '').split(/\n+/)
13 There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.
14 When in doubt, use brute force.
15 Deleted code is debugged code.
16 Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
17 The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.
18 Controlling complexity is the essence of computer programming.
19 UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things.
20 Beware of those who say that their software does not suck, for they are either fools or liars.
21 Unix is simple. It just takes a genius to understand its simplicity.
22 If you want to go somewhere, goto is the best way to get there.
23 I object to doing things that computers can do.
24 A good way to have good ideas is by being unoriginal.
25 a program is like a poem: you cannot write a poem without writing it.
28 def headers t, path, score, contents
29 name = (request.params['name'] || File.basename(path)).
32 'Content-Disposition' => "filename=\"#{name}\"",
33 'Fortune' => Fortunes.sample,
34 }.merge!(t.metadata(score) || {}).tap { |h|
36 h['Content-Type'] ||= t.guess_mime(contents) rescue nil
37 h['Content-Length'] ||= contents.bytesize.to_s
45 "++++++++++++[>+++++++++<-]>.+++.---."
50 # We can cheat a little with closures because there's just one "real"
51 # endpoint. No need to memoize.
55 tab = Table.new config
58 p = config.path_fixup(CGI.unescape(env['REQUEST_PATH']))
60 return [404, {}, ["404 Not found\nNo such path: #{p}\n"]] unless s
61 [200, headers(tab, p, s, nil), []]
64 p = config.path_fixup(CGI.unescape(env['REQUEST_PATH']))
66 return [404, {}, ["404 Not found\nNo such path: #{p}\n"]] unless s
67 ct = Time.parse(env['HTTP_IF_MODIFIED_SINCE']) rescue nil
69 hs = headers(tab, p, s, nil)
70 mt = Time.parse(hs['Last-Modified']) rescue nil
71 return [304, hs, []] if mt && mt > ct
73 contents = vac.load! s
74 hs = headers(tab, p, s, contents)
75 [200, headers(tab, p, s, contents), [contents]]
79 class App < Watts::App
81 resource(:filename, Serv)
82 resource(['media', :l1], Serv) { resource(:l2, Serv) }