stellaserver 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/stellaserver.rb +71 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6804bc4ed631dc436eac1ad87036bcd91f1daee5
4
+ data.tar.gz: 60c0927aa1453742c1e6ba39b16a3be0bf3fb21f
5
+ SHA512:
6
+ metadata.gz: 9d68f35cd206f6902325b66db7d3cca408145d0f6b2e52fd59ca18698a936e9676dac48abea2b67d25ff418e98bf43639386fde3bf8b4619d85a99cd1c92e1ca
7
+ data.tar.gz: f7cfc75068946a241115ee3cecde41aca19908c3d5693e507cd6ab0a8584dd27af787a6a396b536c47c4827b2cacdb50e0e94d42269f9d6c17ffc80f5bdef9bf
@@ -0,0 +1,71 @@
1
+ require 'socket'
2
+ require 'uri'
3
+
4
+ WEB_ROOT = './public'
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', 2345)
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
+ IO.copy_stream(file, socket)
57
+ end
58
+ else
59
+ message = "File not fount\n"
60
+
61
+ socket.print "HTTP/1.1 404 Not Found\r\n" +
62
+ "Content-Type: text/plain\r\n" +
63
+ "Content-Length: #{message.size}\r\n" +
64
+ "Connection: close\r\n"
65
+ socket.print "\r\n"
66
+
67
+ socket.print message
68
+ end
69
+
70
+ socket.close
71
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stellaserver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kayla Morrison
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
14
+ email: kaylas.email.address@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/stellaserver.rb
20
+ homepage:
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.0
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: the world's cutest server
44
+ test_files: []