simple-server 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4aff57ece9cb73dab5c4d3475c0c79275844105
4
+ data.tar.gz: 7f185740f29518ac608955cb6cd2dbdfed9845f5
5
+ SHA512:
6
+ metadata.gz: a578b17f6f9d145ae0bf8eab1ef352a6091b0f713a4d281ff136f8c5e746796b45b71581e56d338ad0ad389783e8d29000b7468e5ce187a15f6f370c37c52898
7
+ data.tar.gz: f18fa071650f3a79f5aa7f6c71e62cfeffbbc08fce8dcbf26ceaa94f3acb3ac93febb86e0ffd08cb2310c5c696585a4fcacf88e6c2097b562b77c9732a1d38cd
@@ -0,0 +1,37 @@
1
+ *.gem
2
+ *.rbc
3
+ .DS_Store
4
+ /.config
5
+ /coverage/
6
+ /InstalledFiles
7
+ /pkg/
8
+ /spec/reports/
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /test/site
12
+ /test/resources
13
+ /tmp/
14
+
15
+ ## Specific to RubyMotion:
16
+ .dat*
17
+ .repl_history
18
+ build/
19
+
20
+ ## Documentation cache and generated files:
21
+ /.yardoc/
22
+ /_yardoc/
23
+ /doc/
24
+ /rdoc/
25
+
26
+ ## Environment normalisation:
27
+ /.bundle/
28
+ /lib/bundler/man/
29
+
30
+ # for a library or gem, you might want to ignore these files since the code is
31
+ # intended to run in multiple environments; otherwise, check them in:
32
+ Gemfile.lock
33
+ .ruby-version
34
+ .ruby-gemset
35
+
36
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
37
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple-server.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 jslabovitz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,68 @@
1
+ require 'sinatra/base'
2
+ require 'addressable'
3
+ require 'path'
4
+ require 'pp'
5
+
6
+ class SimpleServer < Sinatra::Base
7
+
8
+ VERSION = '0.3'
9
+
10
+ configure do
11
+ enable :logging
12
+ enable :multihosting
13
+ end
14
+
15
+ before do
16
+ env['rack.logger'] = Logger.new(STDERR)
17
+ end
18
+
19
+ def path_for_uri(uri)
20
+ path = Path.new(settings.root)
21
+ path /= (uri.normalized_host ||= settings.default_host).sub(/^(www|web).*?\./, '') if settings.multihosting
22
+ path /= Path.new(Addressable::URI.unencode_component(uri.normalized_path)).relative_to('/')
23
+ path /= 'index.html' if uri.normalized_path[-1] == '/'
24
+ path
25
+ end
26
+
27
+ def parse_request(request)
28
+ begin
29
+ uri = Addressable::URI.parse(request.url)
30
+ rescue Addressable::URI::InvalidURIError => e
31
+ logger.info "#{uri}: invalid URI (#{e.message}): denying"
32
+ halt 400, 'invalid URI'
33
+ end
34
+ uri.host = request.host
35
+ uri
36
+ end
37
+
38
+ get '*' do
39
+ uri = parse_request(request)
40
+ path = path_for_uri(uri)
41
+ if path.basename.to_s[0] == '.'
42
+ logger.info "#{uri} (#{path}): hidden file: denying"
43
+ halt 403, 'unauthorized'
44
+ elsif path.directory?
45
+ logger.info "#{uri} (#{path}): implicit directory: redirecting"
46
+ return redirect "#{uri.path}/"
47
+ elsif (redirect_path = path.add_extension('.redirect')).exist?
48
+ redirect_uri, redirect_code = redirect_path.read.split(/\s+/, 2)
49
+ redirect_code = redirect_code.to_i unless redirect_code.to_s.empty?
50
+ logger.info "#{uri} (#{path}): redirecting via #{redirect_code} to #{redirect_uri}"
51
+ return redirect redirect_uri, redirect_code
52
+ elsif path.file?
53
+ logger.info "#{uri} (#{path}): direct file: serving"
54
+ return send_file(path)
55
+ else
56
+ %w{.html .htm}.each do |extname|
57
+ p = path.add_extension(extname)
58
+ if p.file?
59
+ logger.info "#{uri} (#{path}): guessed extension #{extname}: serving #{p}"
60
+ return send_file(p)
61
+ end
62
+ end
63
+ end
64
+ logger.info "#{uri} (#{path}): not found: denying"
65
+ halt 404, 'not found'
66
+ end
67
+
68
+ end
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'simple-server', git: 'https://github.com/jslabovitz/simple-server.git'
@@ -0,0 +1 @@
1
+ web: bundle exec puma --threads 2:8 --port ${PORT:-9292} --environment ${RACK_ENV:-production}
@@ -0,0 +1,8 @@
1
+ task :upstart do
2
+ sh 'foreman',
3
+ 'export',
4
+ 'upstart',
5
+ '--app', 'simple-server',
6
+ '--user', ENV['USER'],
7
+ '/tmp'
8
+ end
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift '../lib'
2
+ require 'simple-server'
3
+
4
+ SimpleServer.set :root, File.dirname(__FILE__) + '/sites'
5
+ SimpleServer.set :default_host, 'a.local'
6
+
7
+ run SimpleServer
@@ -0,0 +1 @@
1
+ this is a text file
@@ -0,0 +1 @@
1
+ Success!
@@ -0,0 +1 @@
1
+ This is subdirectory B of site A.
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <body>
3
+ <p>This is the index file.</p>
4
+ <p>Here is a link to an HTML file without an extension: <a href="another">another file</a>.</p>
5
+ <p>Here is a link to a file with space characters: <a href="a%20file.txt">a text file</a>.</p>
6
+ <p>Here is a link to a directory: <a href="b">a directory</a>.</p>
7
+ <p>Here is a link to an redirect file that will go to the same directory: <a href="c">redirect file</a>.</p>
8
+ </body>
9
+ </html>
@@ -0,0 +1,30 @@
1
+ #encoding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'simple-server'
7
+
8
+ Gem::Specification.new do |s|
9
+ s.name = 'simple-server'
10
+ s.version = SimpleServer::VERSION
11
+ s.summary = 'A simple web server.'
12
+ s.author = 'John Labovitz'
13
+ s.email = 'johnl@johnlabovitz.com'
14
+ s.description = %q{
15
+ SimpleServer provides a simple web server.
16
+ }
17
+ s.homepage = 'http://github.com/jslabovitz/simple-server'
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_path = 'lib'
22
+
23
+ s.add_dependency 'addressable'
24
+ s.add_dependency 'path'
25
+ s.add_dependency 'sinatra'
26
+ s.add_dependency 'puma'
27
+
28
+ s.add_development_dependency 'bundler'
29
+ s.add_development_dependency 'rake'
30
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-server
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.3'
5
+ platform: ruby
6
+ authors:
7
+ - John Labovitz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: path
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sinatra
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: puma
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: "\n SimpleServer provides a simple web server.\n "
98
+ email: johnl@johnlabovitz.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - Gemfile
105
+ - LICENSE
106
+ - Rakefile
107
+ - lib/simple-server.rb
108
+ - sample/Gemfile
109
+ - sample/Gemfile.lock
110
+ - sample/Procfile
111
+ - sample/Rakefile
112
+ - sample/config.ru
113
+ - sample/sites/localhost/a file.txt
114
+ - sample/sites/localhost/another.html
115
+ - sample/sites/localhost/b/index.html
116
+ - sample/sites/localhost/c.redirect
117
+ - sample/sites/localhost/index.html
118
+ - simple-server.gemspec
119
+ homepage: http://github.com/jslabovitz/simple-server
120
+ licenses: []
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.5.2
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: A simple web server.
142
+ test_files: []