rubyserver 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/rubyserver.rb +75 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e4b81bfcc7a6c28178a51e284696f98640e0422e
|
4
|
+
data.tar.gz: 2d3d3dd440e3d4b9d23ef4ff8c26d399dc5eb3be
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c9051fcaffb83283f58b92d583df211452526a53f74560c3c47f7b1203d46a37497366927e280be9a84d55ef9fe2f3f4837756f65539706ccda5d41b5012262
|
7
|
+
data.tar.gz: f13b89c4fa12c54e128257e317b6b27b22b7a823dabc10b0e4432a3ebc9442411325b6510dbe027f8e8a9714a419f91b7501b2a8d695d3edef8c90b43ed0f3d9
|
data/lib/rubyserver.rb
ADDED
@@ -0,0 +1,75 @@
|
|
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/jpg'
|
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
|
+
parts = path.split("/")
|
26
|
+
|
27
|
+
parts.each do |part|
|
28
|
+
next if part.empty? || part == '.'
|
29
|
+
part == '..' ? clean.pop : clean << part
|
30
|
+
end
|
31
|
+
|
32
|
+
File.join(WEB_ROOT, *clean)
|
33
|
+
end
|
34
|
+
|
35
|
+
server = TCPServer.new('localhost', 6789) # Server bound to port 6789
|
36
|
+
|
37
|
+
loop do
|
38
|
+
begin
|
39
|
+
socket = server.accept # Wait for a client to connect
|
40
|
+
request_line = socket.gets
|
41
|
+
|
42
|
+
STDERR.puts request_line
|
43
|
+
|
44
|
+
path = requested_file(request_line)
|
45
|
+
path = File.join(path, 'index.html') if File.directory?(path)
|
46
|
+
|
47
|
+
if File.exist?(path) && !File.directory?(path)
|
48
|
+
File.open(path, "rb") do |file|
|
49
|
+
socket.print "HTTP/1.1 200 OK\r\n" +
|
50
|
+
"Content-Type: #{content_type(file)}\r\n" +
|
51
|
+
"Content-Length: #{file.size}\r\n" +
|
52
|
+
"Connection: close\r\n"
|
53
|
+
socket.print "\r\n"
|
54
|
+
IO.copy_stream(file, socket)
|
55
|
+
end
|
56
|
+
else
|
57
|
+
message = "So sorry, file not found\n"
|
58
|
+
socket.print "HTTP/1.1 404 Not Found\r\n" +
|
59
|
+
"Content-Type: text/plain\r\n" +
|
60
|
+
"Content-Length: #{message.size}\r\n" +
|
61
|
+
"Connection: close\r\n"
|
62
|
+
socket.print "\r\n"
|
63
|
+
socket.print message
|
64
|
+
end
|
65
|
+
rescue Exception
|
66
|
+
message = "WAT! 500 ERROR! TRY AGAIN LATER SUCKAH!\n"
|
67
|
+
socket.print "HTTP/1.1 500 Internal Server Error\r\n" +
|
68
|
+
"Content-Type: text/plain\r\n" +
|
69
|
+
"Content-Length: #{message.size}\r\n" +
|
70
|
+
"Connection: close\r\n"
|
71
|
+
socket.print "\r\n"
|
72
|
+
socket.print message
|
73
|
+
end
|
74
|
+
socket.close
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubyserver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- chartreauxx
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Really the summary says it all...
|
14
|
+
email: chartreauxx@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/rubyserver.rb
|
20
|
+
homepage: http://rubygems.org/gems/rubyserver
|
21
|
+
licenses:
|
22
|
+
- BFD
|
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: Basic Ruby server with 404 and 500 errors that serves up a basic index page.
|
44
|
+
test_files: []
|