红宝石server.rb无法在终端上工作

时间:2018-07-31 09:10:40

标签: ruby

首先让我说我对该主题知识很少。当我运行ruby server.rb时,出现语法错误。我已经解决了第一个错误,但又遇到了另一个错误,所以我不确定发生了什么。

server.rb

require 'socket'
server = TCPServer.new 9999

PAGES = {
  "/" => "Hi, welcome to the home page!",
  "/about" => "About us: we are http hackers",
  "/news" => "We haven't made much news yet with this server, but stay tuned"
}

PAGE_NOT_FOUND = "Page not found type."

loop do
  session = server.accept
  request = []
  while (line = session.gets) && (line.chomp.length > 0)
    request << line.chomp
  end
  puts "finished reading"

  http_method, path, protocol = request[0].split(' ') 

  cookie_header = request.detect { |line| line=~ /^Cookie: / }
  cookie_parts = cookie_header.sub /^Cookie: /,''
    cookie_parts = cookie_parts.split(';')
    cookie_parts = cookie_parts.map { |cookie| cookie.split('=') }
    cookies = Hash[*cookie_parts.flatten]
  else
    cookies = {}
  end

  cookies["session_key"] ||=Time.now.to_f
  count = cookies["session_count"].to_i
  count += 1
  cookies["session_count"] = count



  if PAGES.keys.include? path
    status = "200 OK"
    response_body = PAGES[path]
  else
    status = "404 Not Found"
    response_body = PAGE_NOT_FOUND
  end

  session.puts <<-HEREDOC
HTTP/1.1 #{status}
set-cookie:session_key=#{cookies["session_key"]}
get-cookie:session_count=#{cookies["session_count"]}

#{response_body}
this was visit number #{cookies["session_count"]}
  HEREDOC

  session.close
end

这是第一个错误。

server.rb:27: syntax error, unexpected keyword_else, expecting keyword_end
   else
       ^

server.rb:29: syntax error, unexpected keyword_end, expecting end-of-input
   end
      ^

我认为如果我摆脱第27-29行会得到解决,但我想不是。

0 个答案:

没有答案