From: Andrew Hunt Date: 2000-05-31T11:52:58-04:00 Subject: [ruby-talk:03056] Bug in HTTP close Hi folks, I think I have found a couple of small problems in the network code. 1) In net/protocol.rb, around line 417: def close @socket.close unless @closed ^^^^^^^^^^^^^^ @closed = true end It might be better to add the "unless @closed", as you will get an exception trying to close an already closed socket. How did I find this, you may ask? 2) In net/http.rb, a socket may be closed one of two ways: the socket will be closed if keep_alive? is false the socket will be closed if there's a "connection: close" header. Unfortunately, both of these conditions can happen, which results in the socket being closed twice. Here's some sample code which shows the problem, courtesy of Dave, who has promised to give up sleeping until we finish the book :-) Client: require 'net/http' h = Net::HTTP.new('localhost', '4321') puts "Fetching" resp, data = h.get('/', nil ) puts "Got: #{resp.message}" Server: require 'socket' server = TCPServer.new('localhost', 4321) while (session = server.accept) puts "Request:" loop { a = session.gets a.chomp!.strip! break if a.length == 0 puts a } session.print "HTTP/1.0 200/OK\r\nContent-type: text/html\r\n" session.print "Connection: close\r\n\r\n" session.print "\r\n" session.print "

#{Time.now}

\r\n" session.close end /\ndy -- Andrew Hunt, The Pragmatic Programmers, LLC. Innovative Object-Oriented Software Development web: http://www.pragmaticprogrammer.com email: andy@pragmaticprogrammer.com -- Our New Book: "The Pragmatic Programmer" Published by Addison-Wesley Oct 1999 (see www.pragmaticprogrammer.com/ppbook) --