#!/usr/local/bin/ruby # 2001/07/04 # # # Takashi Shimizu # (c)2001 Takashi Shimizu # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2, or (at # your option) any later version. # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. require "socket" require "getopts" Service={"http"=>80} def parseurl(url) #=> [protocol, host, port, location] url.scan(%r!(\w+)://([^/:]+)(:\d+)?(\S*)!){|protocol,host,port,loc| #/ loc="/" unless loc if port;port=port[1..-1].to_i;else port=Service[protocol];end return protocol,host,port,loc } end class HTTPGet def initialize(sock) @sock=sock @header={} @commandline="" @headerdata="" @body="" @command=""; @url=""; @httpversion=""; @protocol="" @host=""; @port=""; @location="" @buffer="" ;@prompt; @peer=nil end attr_accessor :host, :port, :prompt, :peer, :url, :header def HTTPGet.readsock(sock, prompt=">", peer=nil) http=new(sock) http.peer=peer http.prompt=prompt http.getheader{|line| yield line if block_given? } http.getbody{|line| yield line if block_given? } http end def getheader while not (@commandline=@sock.gets); ;end STDERR.print(@prompt, @commandline) unless $quiet yield @commandline if block_given? while line=@sock.gets @headerdata< ",req.url){|str| sock.print str } rescue SystemCallError STDERR.print "#{th} terminated with #{$!}\n" unless $quiet rescue STDERR.puts $! unless $quiet ensure sockr.close if sockr sock.close if sock STDERR.print "#{th} finished\n" unless $quiet if (not $quiet) && (stack=ThreadGroup::Default.list).size>2 stack=(stack-[thparent,Thread.current]).map{|t|t.id} STDERR.print("["+stack.join(" ")+"]\n") end STDERR.flush unless $quiet end end end end end Proxy.main()