Simple TCPSocket server in Ruby exhibits a HTTP header issue
I'm benchmarking some simple HTTP server implementations in Ruby (no threads, threaded, fibers and eventmachine) but this simple piece of code fails using threads:
#!/usr/bin/env ruby
require 'socket'
server = TCPServer.new("127.0.0.1", 8080)
puts "Listening on 127.0.0.1:8080"
while true
Thread.new(server.accept) do |client|
msg = client.readline
headers = [
"",
"HTTP/1.1 200 OK",
"Date: Fri, 30 Sep 2011 08:11:27 GMT",
"Server: TCP socket test",
"Content-Type: text/html; charset=iso-8859-1",
"Content-Len开发者_开发问答gth: #{msg.length}\r\n\r\n"].join("\r\n")
client.write headers
client.write ">>> Data sent:\n #{msg}"
client.close
end
end
A simple curl http://localhost:8080/ works fine, when the first element in the array is "" or other String, but not the "HTTP/1.1 200 OK" response directly. Why is this?
精彩评论