Class: HTTPServer

Inherits:
Object show all
Defined in:
lib/rwd/net.rb

Class Method Summary collapse

Class Method Details

.authenticate(auth, realm, req, resp) ⇒ Object


838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
# File 'lib/rwd/net.rb', line 838

def self.authenticate(auth, realm, req, resp)
  if auth.kind_of? String
    file	= "#{home}/#{auth}"
    auths	= {}
    auths	= Hash.file(file)	if File.file?(file)
  else
    auths	= auth
  end

  authuserpassword	= req["authorization"]
  if not authuserpassword.nil?
    authtype, userpassword	= authuserpassword.split(/ /)
    if authtype == "Basic" and not userpassword.nil?
      u, p	= userpassword.unpack("m").shift.split(/:/)
    end
  end

  ok	= (auths.include?(u) and auths[u] == p)

  unless ok
    resp["WWW-Authenticate"]	= "Basic realm=\"#{realm}\""
    resp.response		= "HTTP/1.0 401 Unauthorized"
  end

  req.user	= u

  return ok
end

.serve(portio = 80, remote = false, auth = nil, realm = "ev/net") ⇒ Object


732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/rwd/net.rb', line 732

def self.serve(portio=80, remote=false, auth=nil, realm="ev/net")
  port, server	= portio

  begin
    server	= TCPServer.new(remote ? "0.0.0.0" : "localhost", port)	if server.nil?

    $stderr.puts "Just point your browser to http://localhost:#{port}/ ..."
  rescue
    server	= nil

    $stderr.puts "Port #{port} is in use."
  end

  if not server.nil?
    count	= 0

    at_exit do
      #$stderr.puts "Received #{count} requests"
    end

    serverthread =
    Thread.new do
      mutex	= Mutex.new

      Thread.current["threads"]	= []

      every(1, Thread.current) do |thread|
        mutex.synchronize do
          thread["threads"].delete_if{|t| (not t.alive?)}
        end
      end

      loop do
        io	= server.accept
        count += 1

        thread =
        Thread.new(Thread.current, count) do |parentthread, count2|
          stop	= false

          begin
            begin
              req	= Request.new(io)
              resp	= Response.new(io)
            rescue
              raise HTTPServerException
            end

            begin
              ip	= req.peeraddr[3]
            rescue NameError
              raise HTTPServerException
            end

            if (not remote) or (remote and (auth.nil? or auth.empty? or authenticate(auth, realm, req, resp)))
          #    $stderr.puts "#{count2} #{Time.new.strftime("%Y-%m-%d %H:%M:%S")} #{ip} #{req.user} #{req.request.to_s.strip}"
           1 == 1
              begin
                yield(req, resp)
              rescue Exception => e
                mutex.synchronize do
                  $stderr.puts e.class.to_s + ": " + e.message
                  $stderr.puts e.backtrace.collect{|s| "\t"+s}.join("\n")
                end
                resp["Content-Type"]	= "text/plain"
                resp.response		= "HTTP/1.0 200 ???"
                resp.clean
                resp << e.class.to_s + ": " + e.message
                resp << "\n"
                resp << "\n"
                resp << e.backtrace.collect{|s| "\t"+s}.join("\n")
                resp << "\n"
                resp << "\n"
                resp << "(You can use the back button and stop the application properly, if appropriate.)"
              end

              stop	= true	if resp.stop?
            end

            begin
              resp.flush			
            rescue
              raise HTTPServerException
            end
          rescue HTTPServerException
          end

          parentthread["stop"]	= resp	if stop
        end

        mutex.synchronize do
          Thread.current["threads"] << thread
        end
      end
    end

    sleep 0.1	while not serverthread["stop"]

    serverthread["threads"].each {|t| t.join}

    serverthread["stop"].at_stop.call

    serverthread.kill
  end
end