ugotserver 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/ugotserver +4 -0
  3. data/lib/ugotserver.rb +78 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a7242a87274e2614ae8c5bf802e4960a3c8b893
4
+ data.tar.gz: eb4a1a2258126969309cd3d9a7e3fb2797466d75
5
+ SHA512:
6
+ metadata.gz: 4b9508d4117f99c0c06a4a587786dafe890ca0fef49a45ddbf898d106954a527e1c1c2190918f3942c34dd68ca8ead4a540bdfe5c852ed10a35c7a3712db969b
7
+ data.tar.gz: 361345866c76f21ea14993412d3b734174273e67a898b8c3238c5fb7d4591097c6c0e984eda64a3a7193d7d507746d84bc2659db8e1f940c28d3add229a6f9e7
data/bin/ugotserver ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ugotserver'
4
+ UgotServer.new(ARGV[0])
data/lib/ugotserver.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'socket'
2
+ require 'uri'
3
+
4
+ class UgotServer
5
+ def initialize(port = 1870)
6
+ @WEB_ROOT = File.expand_path "../../..", __FILE__
7
+ @server = TCPServer.new('localhost',port)
8
+
9
+ @CONTENT_TYPE_MAPPING = {
10
+ 'html' => 'text/html',
11
+ 'txt' => 'text/plain',
12
+ 'png' => 'image/png',
13
+ 'jpg' => 'image/jpeg'
14
+ }
15
+
16
+ @DEFAULT_CONTENT_TYPE = 'application/octet-stream'
17
+ loop_it
18
+ end
19
+
20
+ def content_type(path)
21
+ ext = File.extname(path).split(".").last
22
+ @CONTENT_TYPE_MAPPING.fetch(ext, @DEFAULT_CONTENT_TYPE)
23
+ end
24
+
25
+ def requested_file(request_line)
26
+ request_uri = request_line.split(" ")[1]
27
+ path = URI.unescape(URI(request_uri).path)
28
+ clean = []
29
+
30
+ parts = path.split("/")
31
+
32
+ parts.each do |part|
33
+ next if part.empty? || part == '.'
34
+ part == '..' ? clean.pop : clean << part
35
+ end
36
+
37
+ File.join(@WEB_ROOT, *clean)
38
+ end
39
+
40
+ def loop_it
41
+ loop do
42
+ socket = @server.accept
43
+ request_line = socket.gets
44
+
45
+ STDERR.puts request_line
46
+
47
+ path = requested_file(request_line)
48
+
49
+ path = File.join(path, 'index.html') if File.directory?(path)
50
+
51
+ if File.exist?(path) && !File.directory?(path)
52
+ File.open(path, 'rb') do |file|
53
+ socket.print "HTTP/1.1 200 OK\r\n" +
54
+ "Content-Type: #{content_type(file)}\r\n" +
55
+ "Content-Length: #{file.size}\r\n" +
56
+ "Connection: close\r\n"
57
+
58
+ socket.print "\r\n"
59
+
60
+ IO.copy_stream(file, socket)
61
+ end
62
+ else
63
+ message = "File Not Found\n"
64
+
65
+ socket.print "HTTP/1.1 404 Not Found\r\n" +
66
+ "Content-Type: text/plain\r\n" +
67
+ "Content-Length: #{message.size}\r\n" +
68
+ "Connection: close\r\n"
69
+
70
+ socket.print "\r\n"
71
+
72
+ socket.print message
73
+ end
74
+
75
+ socket.close
76
+ end
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ugotserver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Craig Lucksted
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 with custom default pages
14
+ email: luckstedcraig@gmail.com
15
+ executables:
16
+ - ugotserver
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/ugotserver
21
+ - lib/ugotserver.rb
22
+ homepage: http://rubygems.org/gems/ugotserver
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Simple serve
46
+ test_files: []