diff --git a/features/server.feature b/features/server.feature index 079a5c8..d433529 100644 --- a/features/server.feature +++ b/features/server.feature @@ -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" diff --git a/lib/yarn/abstract_handler.rb b/lib/yarn/abstract_handler.rb index 26d8a6f..9f2bb26 100644 --- a/lib/yarn/abstract_handler.rb +++ b/lib/yarn/abstract_handler.rb @@ -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 diff --git a/lib/yarn/request_handler.rb b/lib/yarn/request_handler.rb index 1242fc1..c386ad9 100644 --- a/lib/yarn/request_handler.rb +++ b/lib/yarn/request_handler.rb @@ -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)