0

编辑:发现了一些有趣的东西。CSS 适用于 IE8,但不适用于 Firefox。我猜这是字符编码问题?

我正在尝试学习如何编写自己的 Web 服务器,并且我设法让服务器启动并运行,但由于某种原因它不会显示 css。然而,Javascript 工作得很好。css 文件已加载,但样式未显示在 Firebug 中。谁能告诉我我在这里做错了什么?

服务器的代码如下所示。要运行服务器,您只需调用“启动”一个空列表。或者,如果需要,您可以在此处提取整个代码库:https ://github.com/dm9600/webserver

require 'socket'

def start(args)
   webserver = create_server args
   basepath = './app/'

   while (session = webserver.accept)
      puts "HTTP/1.1 200/OK\nContent-type:text/html\n\n"
      session.print "HTTP/1.1 200/OK\nContent-type:text/html\n\n"
      request = session.gets
      puts "request" + request
      trimmedrequest = trim_request(request)
      filename = trimmedrequest.chomp
      begin
         displayfile = find_file(filename)
         content = displayfile.read()
         session.print content
      rescue Errno::ENOENT
         session.print "File not found"
      end
      session.close
   end
end 

def create_server(args)
   command = args[0]
   #default port is going to be 3333
   port = 3333
   #default address will be localhost
   address = "localhost"
   port = args[1] if args[0].instance_of? String and args[0].eql? "p"
   puts "Server created at #{address} and port #{port}"
   TCPServer.new address, port
end 

def trim_request(request)
   request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
end 

def find_file(path)
   basepath = "./app/"
   if path.empty?
      full_path = basepath + 'index.html'
   else
      full_path = basepath + path
   end 
   File.open full_path, 'r'
end 

我正在运行的 html。它位于 /app/index.html 中。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <script src="js/application.js"></script>
      <title>CSS-Test</title>

      <link type="text/css" href="css/application.css" rel="stylesheet"></link>
   </head>
   <body>

      <h1>CSS-Test</h1>

      <div id="box-one">
         <p>This is box one.</p>
      </div>

      <div id="box-two">
         <p>This is box two.</p>
      </div>

   </body>
</html>

我正在尝试运行的 css。它位于 /app/css/application.css

.someclass {
   background: blue;
}

* {
   background: black;
}

编辑:这里有错误的 css 文件位置。

4

1 回答 1

0

正如评论中指出的“mu太短”,我犯了一个巨大的新手错误,将所有东西都用 发送回来Content-type: text/html,这显然是一个大问题!我所要做的就是将正确的文件与正确的内容类型相关联,然后一切正常。这是固定server.rb文件:

require 'socket'

def start(args)
   webserver = create_server args
   basepath = './app/'

   while (session = webserver.accept)
      request = session.gets
      trimmedrequest = trim_request(request)
      ct = get_content_type trimmedrequest
      session.print "HTTP/1.1 200/OK\nContent-type:#{ct}\n\n"
      puts"HTTP/1.1 200/OK\nContent-type:#{ct}\n\n" 
      filename = trimmedrequest.chomp
      begin
         displayfile = find_file(filename)
         content = displayfile.read()
         session.print content
      rescue Errno::ENOENT
         session.print "File not found"
      end
      session.close
   end
end 

def create_server(args)
   command = args[0]
   #default port is going to be 3333
   port = 3333
   #default address will be localhost
   address = "localhost"
   port = args[1] if args[0].instance_of? String and args[0].eql? "p"
   puts "Server created at #{address} and port #{port}"
   TCPServer.new address, port
end 

def trim_request(request)
   request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
end 

def find_file(path)
   basepath = "./app/"
   if path.empty?
      full_path = basepath + 'index.html'
   else
      full_path = basepath + path
   end 
   File.open full_path, 'r'
end 

def get_content_type(path)
    ext = File.extname(path)
    return "text/html"  if ext.include? ".html" or ext.include? ".htm"
    return "text/plain" if ext.include? ".txt"
    return "text/css"   if ext.include? ".css"
    return "image/jpeg" if ext.include? ".jpeg" or ext.include? ".jpg"
    return "image/gif"  if ext.include? ".gif"
    return "image/bmp"  if ext.include? ".bmp"
    return "text/plain" if ext.include? ".rb"
    return "text/xml"   if ext.include? ".xml"
    return "text/xml"   if ext.include? ".xsl"
    return "text/html"
end
于 2013-07-01T19:51:28.553 回答