httpserver 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +25 -0
- data/Rakefile +1 -0
- data/bin/httpserver +4 -0
- data/httpserver.gemspec +23 -0
- data/lib/httpserver.rb +4 -0
- data/lib/httpserver/mime.rb +18 -0
- data/lib/httpserver/response.rb +20 -0
- data/lib/httpserver/version.rb +3 -0
- data/lib/httpserver/webserver.rb +62 -0
- metadata +92 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mark Gandolfo
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Httpserver
|
2
|
+
|
3
|
+
Start a http server in any directory
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem install httpserver
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
go to any directory
|
14
|
+
|
15
|
+
`httpserver .`
|
16
|
+
|
17
|
+
will start a webserver in that directory
|
18
|
+
|
19
|
+
## Contributing
|
20
|
+
|
21
|
+
1. Fork it
|
22
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
23
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
24
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
25
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/httpserver
ADDED
data/httpserver.gemspec
ADDED
@@ -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 'httpserver/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "httpserver"
|
8
|
+
spec.version = Httpserver::VERSION
|
9
|
+
spec.authors = ["Mark Gandolfo"]
|
10
|
+
spec.email = ["mark@aussiev8.com.au"]
|
11
|
+
spec.description = %q{Start a http server in any directory}
|
12
|
+
spec.summary = %q{Start a http server in any directory with one simple command}
|
13
|
+
spec.homepage = "https://github.com/markgandolfo/httpserver"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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", "httpserver"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/httpserver.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Httpserver
|
2
|
+
module Mime
|
3
|
+
def get_content_type(path)
|
4
|
+
ext = File.extname(path)
|
5
|
+
return "text/html" if ext == ".html" or ext == ".htm"
|
6
|
+
return "text/plain" if ext == ".txt"
|
7
|
+
return "text/css" if ext == ".css"
|
8
|
+
return "image/jpeg" if ext == ".jpeg" or ext == ".jpg"
|
9
|
+
return "image/gif" if ext == ".gif"
|
10
|
+
return "image/png" if ext == ".png"
|
11
|
+
return "image/bmp" if ext == ".bmp"
|
12
|
+
return "text/plain" if ext == ".rb"
|
13
|
+
return "text/xml" if ext == ".xml"
|
14
|
+
return "text/xml" if ext == ".xsl"
|
15
|
+
return "text/html"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Httpserver
|
2
|
+
module Response
|
3
|
+
def response(attrs)
|
4
|
+
code = attrs[:code].to_i
|
5
|
+
mime = attrs[:mime] || "text/html"
|
6
|
+
content = attrs[:content] || ""
|
7
|
+
status = ""
|
8
|
+
|
9
|
+
case(code)
|
10
|
+
when 200
|
11
|
+
status = "OK"
|
12
|
+
when 404
|
13
|
+
status = "Object Not Found"
|
14
|
+
return "HTTP/1.1 #{code}/#{status}\r\nServer HttpServer\r\n\r\n#{content}"
|
15
|
+
end
|
16
|
+
|
17
|
+
"HTTP/1.1 #{code}/#{status}\r\nContent-type:#{mime}\r\n\r\n#{content}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'socket'
|
2
|
+
module Httpserver
|
3
|
+
class WebServer
|
4
|
+
include Mime
|
5
|
+
include Response
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
port = ARGV.first.to_i
|
9
|
+
port = 8080 if port == 0
|
10
|
+
webserver = TCPServer.new('0.0.0.0', port)
|
11
|
+
puts "Listening on http://0.0.0.0:#{port}"
|
12
|
+
puts "Ctrl + c will break"
|
13
|
+
base_dir = Dir.new(".")
|
14
|
+
|
15
|
+
while (session = webserver.accept)
|
16
|
+
request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '').chomp
|
17
|
+
resource = request
|
18
|
+
resource = "." if resource == ""
|
19
|
+
|
20
|
+
# Is the user trying to go to a file that doesn't exist?
|
21
|
+
if !File.exists?(resource)
|
22
|
+
session.print response(code:404, content:"404 - Resource cannot be found.")
|
23
|
+
session.close
|
24
|
+
next
|
25
|
+
end
|
26
|
+
|
27
|
+
# if the resource is a directory, lets print out the contents
|
28
|
+
if File.directory?(resource)
|
29
|
+
session.print response(code:200)
|
30
|
+
base_dir = Dir.new("./#{request}")
|
31
|
+
|
32
|
+
base_dir.entries.each do |file|
|
33
|
+
dir_sign = ""
|
34
|
+
base_path = resource + "/"
|
35
|
+
base_path = "" if resource == ""
|
36
|
+
resource_path = base_path + file
|
37
|
+
dir_sign = "/" if File.directory?(resource_path)
|
38
|
+
|
39
|
+
# If this entry is the ".." then build the breadcrumb directory
|
40
|
+
if file == ".."
|
41
|
+
breadcrumb = "/" + base_path.split('/')[0..-2].join('/')
|
42
|
+
session.print("<a href='#{breadcrumb}'>#{file}</a><br />")
|
43
|
+
else
|
44
|
+
# Otherwise, link to the file
|
45
|
+
session.print("<a href='/#{resource_path}'>#{file}#{dir_sign}</a><br />")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
else
|
49
|
+
# We are looking at a piece of content here!
|
50
|
+
session.print response(:code => 200, :mime => get_content_type(resource))
|
51
|
+
File.open(resource, "rb") do |file|
|
52
|
+
while (!file.eof?) do
|
53
|
+
buffer = file.read(256)
|
54
|
+
session.write(buffer)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
session.close
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: httpserver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mark Gandolfo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Start a http server in any directory
|
47
|
+
email:
|
48
|
+
- mark@aussiev8.com.au
|
49
|
+
executables:
|
50
|
+
- httpserver
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- bin/httpserver
|
60
|
+
- httpserver.gemspec
|
61
|
+
- lib/httpserver.rb
|
62
|
+
- lib/httpserver/mime.rb
|
63
|
+
- lib/httpserver/response.rb
|
64
|
+
- lib/httpserver/version.rb
|
65
|
+
- lib/httpserver/webserver.rb
|
66
|
+
homepage: https://github.com/markgandolfo/httpserver
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
- httpserver
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.25
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Start a http server in any directory with one simple command
|
92
|
+
test_files: []
|