ws 0.3.0 → 0.4.0
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.
- data/README.md +2 -3
- data/bin/ws +1 -1
- data/lib/ws.rb +18 -16
- metadata +1 -1
data/README.md
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
ws(3) -- Simple Local Web Server
|
2
|
-
=============================
|
1
|
+
# ws(3) -- Simple Local Web Server
|
3
2
|
|
4
3
|
## SYNOPSIS
|
5
4
|
:::text
|
6
|
-
ws [-v | --verbose] [-d | --directory DIRECTORY] [-p | --port PORT] [-h | --help]
|
5
|
+
ws [-v | --verbose] [-d | --directory DIRECTORY] [-p | --port PORT] [-i | --index INDEXFILE] [-h | --help]
|
7
6
|
|
8
7
|
## INSTALLTION
|
9
8
|
|
data/bin/ws
CHANGED
data/lib/ws.rb
CHANGED
@@ -3,23 +3,25 @@ require "socket"
|
|
3
3
|
module WS
|
4
4
|
def self.start_server(options)
|
5
5
|
server = TCPServer.new("127.0.0.1", options[:port])
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
6
|
+
loop do
|
7
|
+
Thread.start(server.accept) do |conn|
|
8
|
+
file = conn.gets.scan(/(?<=\/).*(?=\sHTTP)/)[0].split("/")
|
9
|
+
file_path = File.join(options[:directory], file)
|
10
|
+
file_path = File.join(file_path, options[:index]) if File.directory? file_path
|
11
|
+
mime_type = get_mime_type file_path
|
12
|
+
begin
|
13
|
+
file_handle = File.open(file_path, "r")
|
14
|
+
puts "#{Time.now.inspect}: Sending #{file_path} to client." if options[:verbose]
|
15
|
+
conn.print get_header("200/OK", mime_type)
|
16
|
+
conn.print file_handle.read()
|
17
|
+
file_handle.close
|
18
|
+
rescue Errno::ENOENT
|
19
|
+
puts "#{Time.now.inspect}: Not Found: #{file_path}" if options[:verbose]
|
20
|
+
conn.print get_header("404/NOT FOUND", "text/html")
|
21
|
+
conn.print "404: File not found."
|
22
|
+
end
|
23
|
+
conn.close
|
21
24
|
end
|
22
|
-
conn.close
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|