Permalink
Browse files

Handler refactorings. Wording in server.feature

  • Loading branch information...
1 parent 47509f8 commit 902aaba8d5aea62fe1fade319a16d251d87f23b8 Jesper Kjeldgaard committed Oct 9, 2011
Showing with 25 additions and 86 deletions.
  1. +2 −6 features/server.feature
  2. +14 −71 lib/yarn/abstract_handler.rb
  3. +9 −9 lib/yarn/request_handler.rb
View
@@ -1,8 +1,8 @@
Feature: Server control
As a developer
- I want to be able to command the server
- So that I can control the server
+ I want to be able to start and stop
+ So that I can serve files to the web
Scenario: Start server
When I start the server on port 3000
@@ -12,7 +12,3 @@ Feature: Server control
Given the server is running
When I stop the server
Then I should see "Server stopped"
-
- Scenario: Supply port number when starting the server
- When I start the server on port 4000
- Then I should see "Server started on port 4000"
@@ -52,10 +52,6 @@ def parse_request
def prepare_response
end
- def post_body
- @request ? @request[:body].to_s : ""
- end
-
def return_response
@session.puts "HTTP/1.1 #{@response.status} #{STATUS_CODES[@response.status]}"
@session.puts @response.headers.map { |k,v| "#{k}: #{v}" }
@@ -78,13 +74,13 @@ def read_request
input = []
while (line = @session.gets) do
length = line.gsub(/\D/,"") if line =~ /Content-Length/
- if line == "\r\n"
- input << line
- input << @session.read(length.to_i) if length
- break
- else
- input << line
- end
+ if line == "\r\n"
+ input << line
+ input << @session.read(length.to_i) if length
+ break
+ else
+ input << line
+ end
end
@session.close_read
debug "Done reading request"
@@ -105,31 +101,6 @@ def set_common_headers
@response.headers[:Connection] = "Close"
end
- def serve_file(path)
- @response.status = 200
- @response.body << read_file(path)
- @response.headers["Content-Type"] = get_mime_type path
- end
-
- def serve_directory(path)
- @response.status = 200
- if File.exists?("index.html") || File.exists?("/index.html")
- @response.body = read_file "index.html"
- @response.headers["Content-Type"] = "text/html"
- else
- @response.headers["Content-Type"] = "text/html"
- directory_lister = DirectoryLister.new
- @response.body << directory_lister.list(path)
- end
- end
-
- def read_file(path)
- file_contents = []
- File.open(path).each { |line| file_contents << line }
-
- file_contents
- end
-
def extract_path
path = @request[:uri][:path].to_s
if path[0] == "/" && path != "/"
@@ -138,44 +109,16 @@ def extract_path
path.gsub(/%20/, " ").strip
end
- def serve_directory(path)
- @response.status = 200
- if File.exists?("index.html")# || File.exists?("/index.html")
- @response.body = read_file "index.html"
- @response.headers["Content-Type"] = "text/html"
- else
- @response.headers["Content-Type"] = "text/html"
- @response.body << DirectoryLister.list(path)
- end
+ def post_body
+ @request ? @request[:body].to_s : ""
end
- def get_mime_type(path)
- return false unless path.include? '.'
- filetype = path.split('.').last
-
- return case
- when ["html", "htm"].include?(filetype)
- "text/html"
- when "txt" == filetype
- "text/plain"
- when "css" == filetype
- "text/css"
- when "js" == filetype
- "text/javascript"
- when ["png", "jpg", "jpeg", "gif", "tiff"].include?(filetype)
- "image/#{filetype}"
- when ["zip","pdf","postscript","x-tar","x-dvi"].include?(filetype)
- "application/#{filetype}"
- else false
+ def request_path
+ @request[:uri][:path] if @request
end
- end
-
- def request_path
- @request[:uri][:path] if @request
- end
- def client_address
- @session.peeraddr(:numeric)[2] if @session
+ def client_address
+ @session.peeraddr(:numeric)[2] if @session
+ end
end
end
-end
@@ -6,15 +6,10 @@ def prepare_response
@response.headers["Content-Type"] = "text/html"
begin
- if File.directory? path
- serve_directory path
+ if File.directory?(path)
+ serve_directory(path)
elsif File.exists?(path)
- if path =~ /.*\.rb$/
- @response.body << execute_script(path)
- @response.status = 200
- else
- serve_file(path)
- end
+ path =~ /.*\.rb$/ ? serve_ruby_file(path) : serve_file(path)
else
serve_404_page
end
@@ -25,9 +20,14 @@ def prepare_response
end
def serve_file(path)
- @response.status = 200
@response.body << read_file(path)
@response.headers["Content-Type"] = get_mime_type path
+ @response.status = 200
+ end
+
+ def serve_ruby_file(path)
+ @response.body << execute_script(path)
+ @response.status = 200
end
def serve_directory(path)

0 comments on commit 902aaba

Please sign in to comment.