servit 0.0.2

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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/servit.rb +73 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d950deea0664449f0323ae84d369035797e00a50
4
+ data.tar.gz: df8a6a7f6e5626ce81d5c4da691fb84cf678a524
5
+ SHA512:
6
+ metadata.gz: 8b490890b7bf31e10ff4704dd33e9d1b1125cdb694109620acaa25e9c1ce5038b74694dd6dd0be09a73a8cff9252153788a322f97be917769e8445be39558d0a
7
+ data.tar.gz: 83b8363cd4afa645d5ca0f8832d9c87ad8860ec8a5ba0708372b523c7cf158b5de34529bbe0989f7d57e9322bc88cb8c3226182e953023979007412fb4102ebc
data/lib/servit.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'socket'
2
+ require 'uri'
3
+
4
+ WEB_ROOT = './public' unless defined?(WEB_ROOT)
5
+
6
+ CONTENT_TYPE_MAPPING = {
7
+ 'html' => 'text/html',
8
+ 'txt' => 'text/plain',
9
+ 'png' => 'image/png',
10
+ 'jpg' => 'image/jpeg'
11
+ }
12
+
13
+ DEFAULT_CONTENT_TYPE = 'application/octet-stream'
14
+
15
+ def content_type(path)
16
+ ext = File.extname(path).split(".").last
17
+ CONTENT_TYPE_MAPPING.fetch(ext, DEFAULT_CONTENT_TYPE)
18
+ end
19
+
20
+ def requested_file(request_line)
21
+ request_uri = request_line.split(" ")[1]
22
+ path = URI.unescape(URI(request_uri).path)
23
+
24
+ clean = []
25
+
26
+ parts = path.split("/")
27
+
28
+ parts.each do |part|
29
+ next if part.empty? || part == '.'
30
+ part == '..' ? clean.pop : clean << part
31
+ end
32
+
33
+ File.join(WEB_ROOT, *clean)
34
+ end
35
+
36
+ server = TCPServer.new('localhost', 1666)
37
+
38
+ loop do
39
+ socket = server.accept
40
+ request_line = socket.gets
41
+
42
+ STDERR.puts request_line
43
+
44
+ path = requested_file(request_line)
45
+
46
+ path = File.join(path, 'index.html') if File.directory?(path)
47
+
48
+ if File.exist?(path) && !File.directory?(path)
49
+ File.open(path, "rb") do |file|
50
+ socket.print "HTTP/1.1 200 OK\r\n" +
51
+ "Content-Type: #{content_type(file)}\r\n" +
52
+ "Content-Length: #{file.size}\r\n" +
53
+ "Connection: close\r\n"
54
+
55
+ socket.print "\r\n"
56
+
57
+ IO.copy_stream(file, socket)
58
+ end
59
+ else
60
+ message = "File not found\n"
61
+
62
+ socket.print "HTTP/1.1 404 Not Found\r\n" +
63
+ "Content-Type: text/plain\r\n" +
64
+ "Content-Length: #{message.size}\r\n" +
65
+ "Connection: close\r\n"
66
+
67
+ socket.print "\r\n"
68
+
69
+ socket.print message
70
+ end
71
+
72
+ socket.close
73
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: servit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mike Elliott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple server gem that defaults to a page that can crash your computer.
14
+ email: elliotecweb@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/servit.rb
20
+ homepage: http://rubygems.org/gems/servit
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Serve and break
44
+ test_files: []