ninja_server 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a357938b41d37df77fe1178e80b803988e5c8d7
4
+ data.tar.gz: adfcd8a90fa4385141d3bb43572bb095265e4cb9
5
+ SHA512:
6
+ metadata.gz: 5814b38d5f64b1d535160f65fc230f73f96f847126b00709ce3129a979c612d12dbe31c1c36f518f768d2db30616056e339bc8349cc75dfcda8fa8b1a06286b9
7
+ data.tar.gz: cd8bee9e0ca8b4292f4d3b7a9467b0d8f7900f4b5ee54af86b168106b71073dbfecd79ceeb13d7b9c3cdb21cfda491fcf424e495544ccc3ee63c4e867e632422
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ninja_server.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Lillian Ng
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # NinjaServer
2
+
3
+ This is a tiny HTTP server written in Ruby.
4
+
5
+ ### Features
6
+
7
+ * Serves up static files from a directory to a web browser
8
+ * Sanitizes the path requested to prevent access outside the folder
9
+ * Implicitly adds index.html to path when accessing directory roots
10
+ * NinjaServer hides the functionality of TinyServer within it
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'ninja_server'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install ninja_server
25
+
26
+ ## Usage
27
+
28
+ `ninja_server`
29
+ `ninja_server port`
30
+ `ninja_server port staticroot`
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( http://github.com/orangeninjamidget/ninja_server/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
39
+
40
+ ## Author
41
+
42
+ [Lillian Ng](https://github.com/orangeninjamidget/)
43
+
44
+ ## License
45
+
46
+ MIT: [http://lng.mit-license.org](http://lng.mit-license.org)
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task default: 'test'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/*test.rb"
8
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ninja_server'
4
+
5
+ NinjaServer ARGV
@@ -0,0 +1,16 @@
1
+ require "ninja_server/version"
2
+ require "ninja_server/tiny_server"
3
+
4
+ module NinjaServer
5
+ defaultDir = './public' if Dir.exists?('public')
6
+ defaultDir = Dir.pwd unless defaultDir
7
+
8
+ s = TinyServer.new(
9
+ ARGV[0] || 1337,
10
+ ARGV[1] || defaultDir
11
+ )
12
+
13
+ trap('INT') { s.shutdown }
14
+
15
+ s.start
16
+ end
@@ -0,0 +1,78 @@
1
+ require 'socket'
2
+ require 'uri'
3
+
4
+ class TinyServer
5
+ def initialize(port,root)
6
+ @port = port
7
+ @root = root
8
+ end
9
+
10
+ CONTENT_TYPE_MAPPING = {
11
+ 'html' => 'text/html',
12
+ 'txt' => 'text/plain',
13
+ 'png' => 'image/png',
14
+ 'jpg' => 'image/jpeg'
15
+ }
16
+
17
+ DEFAULT_CONTENT_TYPE = 'application/octet-stream'
18
+
19
+ def start
20
+ server = TCPServer.new("localhost", @port) # Server bound to port
21
+
22
+ loop do
23
+ client = server.accept # Wait for a client to connect
24
+ request = client.gets
25
+ STDERR.puts request
26
+ path = requested_file(request)
27
+
28
+ path = File.join(path, 'index.html') if File.directory?(path)
29
+
30
+ if File.exist?(path) && !File.directory?(path)
31
+ File.open(path,"rb") do |file|
32
+ client.print "HTTP/1.1 200 OK\r\n" + #status
33
+ "Content-Type: #{content_type(file)}\r\n" +
34
+ "Content-Length: #{file.size}\r\n" +
35
+ "Connection: close\r\n" +
36
+ "\r\n"
37
+ IO.copy_stream(file,client)
38
+ end
39
+ else
40
+ message = "File not found\n"
41
+ client.print "HTTP/1.1 404 Not Found\r\n" +
42
+ "Content-Type: text/plain\r\n" +
43
+ "Content-Length: #{message.size}\r\n" +
44
+ "Connection: close\r\n" +
45
+ "\r\n"
46
+ client.print message
47
+ end
48
+ client.close
49
+ end
50
+ end
51
+
52
+ def shutdown
53
+ exit
54
+ end
55
+
56
+ private
57
+ # helper method to obtain content type from file extension
58
+ def content_type(path)
59
+ ext = File.extname(path).split('.').last
60
+ CONTENT_TYPE_MAPPING.fetch(ext, DEFAULT_CONTENT_TYPE)
61
+ end
62
+
63
+ # helper method to generate path on server
64
+ def requested_file(request_line)
65
+ request_uri = request_line.split(' ')[1]
66
+ path = URI.unescape(URI(request_uri).path)
67
+
68
+ clean = []
69
+
70
+ parts = path.split('/')
71
+ parts.each do |part|
72
+ next if part.empty? || part == '.'
73
+ part == '..' ? clean.pop : clean << part
74
+ end
75
+
76
+ File.join(@root, *clean)
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module NinjaServer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ninja_server/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ninja_server"
8
+ spec.version = NinjaServer::VERSION
9
+ spec.authors = ["Lillian Ng"]
10
+ spec.email = ["lillian.k.ng@gmail.com"]
11
+ spec.summary = %q{A Tiny Web Server}
12
+ spec.description = %q{A Tiny Ninja Web Server}
13
+ spec.homepage = "https://github.com/orangeninjamidget/ninja_server"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,11 @@
1
+ require_relative "test_helper"
2
+ require "ninja_server"
3
+
4
+ class NinjaServerTest < Minitest::Unit::TestCase
5
+ def setup
6
+ NinjaServer
7
+ end
8
+
9
+ def test_server
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ require "minitest/autorun"
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ninja_server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lillian Ng
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A Tiny Ninja Web Server
42
+ email:
43
+ - lillian.k.ng@gmail.com
44
+ executables:
45
+ - ninja_server
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/ninja_server
55
+ - lib/ninja_server.rb
56
+ - lib/ninja_server/tiny_server.rb
57
+ - lib/ninja_server/version.rb
58
+ - ninja_server.gemspec
59
+ - test/ninja_server_test.rb
60
+ - test/test_helper.rb
61
+ homepage: https://github.com/orangeninjamidget/ninja_server
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.0
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: A Tiny Web Server
85
+ test_files:
86
+ - test/ninja_server_test.rb
87
+ - test/test_helper.rb