ws 0.1.0r1

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.
Files changed (5) hide show
  1. data/README.md +41 -0
  2. data/bin/.wsmimetypes.rb +36 -0
  3. data/bin/ws +54 -0
  4. data/lib/ws.rb +29 -0
  5. metadata +50 -0
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ ws(3) -- Simple Local Web Server
2
+ =============================
3
+
4
+ ## SYNOPSIS
5
+ :::text
6
+ ws \[-v | --verbose\] \[-d | --directory DIRECTORY\] \[-p | --port PORT\] \[-h | --help\]
7
+
8
+ ## INSTALLTION
9
+
10
+ To install, make sure you have the latest version of Ruby and RubyGems installed (tested and working on Ruby 1.9.3, RubyGems 1.8.19) and execute `gem install ws`.
11
+
12
+ ## DESCRIPTION
13
+
14
+ `ws` is a simple script which launches a server for testing of static websites.
15
+
16
+ ## EXAMPLES
17
+
18
+ ### Launch a server with the current directory as the root
19
+
20
+ :::text
21
+ ws
22
+
23
+ ### Launch a server with the current directory as the root, on port 1337
24
+
25
+ :::text
26
+ ws -p 1337
27
+
28
+ ### Launch a server on port 1337, with the home folder as the root, in verbose mode
29
+
30
+ :::text
31
+ ws -d ~ -p 1337 -v
32
+
33
+ ### Show help
34
+
35
+ :::text
36
+ ws -h
37
+
38
+ ## Author
39
+
40
+ Kenneth Powers [`mail@kenpowers.net`](mailto:mail@kenpowers.net)
41
+ [http://bitbucket.org/KenPowers](http://bitbucket.org/KenPowers)
@@ -0,0 +1,36 @@
1
+ module WS
2
+ def self.get_mime_type(filename)
3
+ case filename
4
+ when /^.*\.css$/
5
+ "text/css"
6
+ when /^.*\.gif$/
7
+ "image/gif"
8
+ when /^.*\.html$/
9
+ "text/html"
10
+ when /^.*\.htm$/
11
+ "text/html"
12
+ when /^.*\.jpeg$/
13
+ "image/jpeg"
14
+ when /^.*\.jpg$/
15
+ "image/jpeg"
16
+ when /^.*\.json$/
17
+ "application/json"
18
+ when /^.*\.js$/
19
+ "application/x-javascript"
20
+ when /^.*\.pdf$/
21
+ "application/pdf"
22
+ when /^.*\.png$/
23
+ "image/png"
24
+ when /^.*\.svg$/
25
+ "image/svg+xml"
26
+ when /^.*\.xhtml$/
27
+ "appliaction/xhtml+xml"
28
+ when /^.*\.(xml|xsl)$/
29
+ "application/xml"
30
+ when /^.*\.xslt$/
31
+ "application/xslt+xml"
32
+ else
33
+ "text/plain"
34
+ end
35
+ end
36
+ end
data/bin/ws ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ bin_dir = File.dirname(__FILE__)
3
+ require File.join(bin_dir, "..", "lib", "ws.rb")
4
+ require "optparse"
5
+
6
+ # Set up OptionParser
7
+ options = {}
8
+ optparse = OptionParser.new do |opts|
9
+ opts.banner = "Usage: ws [options]"
10
+
11
+ options[:verbose] = false
12
+ opts.on "-v", "--verbose", "Output more information" do
13
+ options[:verbose] = true
14
+ end
15
+
16
+ options[:directory] = Dir::pwd
17
+ opts.on "-d", "--directory DIRECTORY", "Specify directory to use" do |directory|
18
+ options[:directory] = directory
19
+ end
20
+
21
+ options[:port] = 5000 + options[:directory].hash % 1000
22
+ opts.on "-p", "--port PORT", Integer, "Specify port to use" do |port|
23
+ options[:port] = port
24
+ end
25
+
26
+ opts.on "-h", "--help", "Display this help secreen" do
27
+ puts opts
28
+ exit
29
+ end
30
+ end
31
+
32
+ # Parse options
33
+ optparse.parse!
34
+
35
+ # Place mime type file in home directory if necessary
36
+ file_name = ".wsmimetypes.rb"
37
+ dest_path = File.expand_path(File.join("~", file_name))
38
+ source_path = File.join(bin_dir, file_name)
39
+ unless File.file? dest_path
40
+ require "fileutils"
41
+ FileUtils.cp source_path, dest_path
42
+ end
43
+ require dest_path
44
+
45
+ # Trap Interrupts
46
+ trap("INT") do
47
+ puts "Shutting down..."
48
+ exit
49
+ end
50
+
51
+ # Start server
52
+ puts "Starting server at http://127.0.0.1:#{options[:port]}"
53
+ WS::start_server options
54
+ puts "Server started! Press ctrl-c to quit."
data/lib/ws.rb ADDED
@@ -0,0 +1,29 @@
1
+ require "socket"
2
+
3
+ module WS
4
+ def self.start_server(options)
5
+ server = TCPServer.new("127.0.0.1", options[:port])
6
+ while (conn = server.accept)
7
+ file = conn.gets.scan(/(?<=\/).*(?=\sHTTP)/)[0].split("/")
8
+ file_path = File.join(options[:directory], file)
9
+ file_path = File.join(file_path, "index.html") if File.directory? file_path
10
+ mime_type = get_mime_type file_path
11
+ begin
12
+ file_handle = File.open(file_path, "r")
13
+ puts "#{Time.now.inspect}: Sending #{file_path} to client." if options[:verbose]
14
+ conn.print get_header("200/OK", mime_type)
15
+ conn.print file_handle.read()
16
+ rescue Errno::ENOENT
17
+ puts "#{Time.now.inspect}: Not Found: #{file_path}" if options[:verbose]
18
+ conn.print get_header("404/NOT FOUND", "text/html")
19
+ conn.print "404: File not found."
20
+ end
21
+ conn.close
22
+ end
23
+ end
24
+
25
+ private
26
+ def self.get_header(status, mime_type)
27
+ "HTTP/1.1 #{status}\r\nContent-Type:#{mime_type}\r\n\r\n"
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ws
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0r1
5
+ prerelease: 5
6
+ platform: ruby
7
+ authors:
8
+ - Kenneth Powers
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple web server for testing static websites.
15
+ email:
16
+ - ken@kenpowers.net
17
+ executables:
18
+ - ws
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/ws.rb
23
+ - bin/ws
24
+ - bin/.wsmimetypes.rb
25
+ - README.md
26
+ homepage: http://kenpowers.net
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>'
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.1
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.19
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Launches a simple static web server in the current directory.
50
+ test_files: []