On 22/04/2005, at 11:48 AM, James Britt wrote:

> jm wrote:
>> I gave it a quick run. I had to change
>>  local_host = nil
>> so that it would bind and fixed the line wrap in the posted code. 
>> Telnetted to port 8080 on localhost and hit enter twice and it came 
>> back with a lot of html from slashdot.
>
> I managed the same, on win32, but could not get the results to render 
> in Firefox or IE.  It fetches the page OK (judging by the debugging p 
> calls), but whatever is sent back to the browser is off in some way.
>

Alright. this is wierd I just tried it with firefox on macosx and it 
rendered find, except for all the broken images, etc due to the 
reference to '/', ie

net = Net::HTTP.new(host,80)
response = net.get('/')

Made a few modification. Really needs a clean up and there's probably 
libraries out there which will do this cleaner. I've been staring at 
perl code most of the day wishing it wasn't . Anyway, I've included the 
full script below.

require 'socket'
require 'net/http'
local_host = nil
local_port = 8080

#
# returns proto,host,path or nil
#
def url_split(h)
   reg = Regexp.new('^(\w*)\:\/\/([\w\.]+)(\/.*)')
   md = reg.match(h)
   return nil unless md
   return md[1],md[2],md[3]
end

host = 'slashdot.org'
server = TCPServer.new(local_host,local_port)
while true
   Thread.start(server.accept) do |s|
     header = ''
     data = ''
     # this is a little crude
     header = s.gets                        # added
     while s.gets && !$_.nil?
       data += $_
       break if $_ == "\r\n" || $_ == "\n"
     end
     header = header.split(' ')              # added
     proto,host,path = url_split(header[1])  # added
     p "==== header[1]: #{header[1]}"        # added
     p "==== #{proto} #{host} #{path}"       # added
     p "====Received: #{data}"
     net = Net::HTTP.new(host,80)
     response = net.get(path)                # changed
     s <<  "HTTP/#{response.http_version} 
#{response.code}/#{response.message} \r\n"
     p "====Got back"
     response.each_header do |key,value|
       tmp = key + ": " + value + "\r\n"
      tmp = key + ": " + value + "\r\n"
       p tmp
       s << tmp
     end
     p response.body
     s << "\r\n" << response.body
     s.close # We're done with this request, bring on the next.
   end
end