serve-this 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +25 -0
  2. data/lib/serve-this.rb +59 -0
  3. metadata +82 -0
data/README ADDED
@@ -0,0 +1,25 @@
1
+ serve-this
2
+ ==========
3
+
4
+ Serve the contents of the current directory,
5
+ like it was running on a server.
6
+
7
+ Example
8
+ =======
9
+
10
+ $ serve-this
11
+ *****
12
+ serving ~/some/directory on port 9292
13
+
14
+ Push it to Heroku!
15
+ ==================
16
+
17
+ config.ru
18
+
19
+ require 'serve-this'
20
+ run ServeThis.from(Dir.pwd)
21
+
22
+ Note
23
+ ====
24
+
25
+ this doesnt work yet!
data/lib/serve-this.rb ADDED
@@ -0,0 +1,59 @@
1
+ module ServeThis
2
+
3
+ def self.from(root)
4
+ Rack::Builder.new do
5
+ # ensure we use etags
6
+ use ::Rack::ConditionalGet
7
+ use ::Rack::ETag
8
+
9
+ # we respond to HEAD requests
10
+ use ::Rack::Head
11
+
12
+ app = ServeThis::App.new(root)
13
+ run app
14
+ end.to_app
15
+ end
16
+
17
+ # this does the file serving
18
+ class App
19
+ def initialize(root)
20
+ @root = root
21
+ @file_server = ::Rack::File.new(root)
22
+ end
23
+ attr_reader :root, :file_server
24
+
25
+ def call(env)
26
+ path = env["PATH_INFO"]
27
+
28
+ if forbid?(path)
29
+ forbid!
30
+ else
31
+
32
+ # if we are looking at / lets try index.html
33
+ if path == "/" && File.exist?(File.join(self.root,"index.html"))
34
+ env["PATH_INFO"] = "/index.html"
35
+ end
36
+
37
+ self.file_server.call(env)
38
+ end
39
+ end
40
+
41
+ # prohibit showing system files
42
+ FORBIDDEN = %w( /.git /.gitignore /config.ru )
43
+
44
+ def forbid?(path)
45
+ FORBIDDEN.any? do |forbidden_path|
46
+ path.start_with?(forbidden_path)
47
+ end
48
+ end
49
+
50
+ def forbid!
51
+ body = "Forbidden\n"
52
+ size = Rack::Utils.bytesize(body)
53
+ return [403, {"Content-Type" => "text/plain",
54
+ "Content-Length" => size.to_s,
55
+ "X-Cascade" => "pass"}, [body]]
56
+ end
57
+ end
58
+
59
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serve-this
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Matthew Rudy Jacobs
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-31 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rack
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description:
36
+ email: MatthewRudyJacobs@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - README
45
+ - lib/serve-this.rb
46
+ has_rdoc: true
47
+ homepage: https://github.com/matthewrudy/serve-this
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --main
53
+ - README
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.5.0
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Serve files from the current directory
81
+ test_files: []
82
+