Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 48 lines (41 sloc) 1.436 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
module Veewee
  class Web

    require 'webrick'
    include WEBrick

    class FileServlet < WEBrick::HTTPServlet::AbstractServlet
             
              def initialize(server,localfile)
                super(server)
                @server=server
                @localfile=localfile
              end
             def do_GET(request,response)
                     response['Content-Type']='text/plain'
                     response.status = 200
                     puts "Serving file #{@localfile}"
                     displayfile=File.open(@localfile,'r')
                     content=displayfile.read()
                     response.body=content
                     #If we shut too fast it might not get the complete file
                     sleep 2
                     @server.shutdown
             end
     end

    def self.wait_for_request(filename,options={:timeout => 10, :web_dir => "", :port => 7125})
      
      webrick_logger=WEBrick::Log.new("/dev/null", WEBrick::Log::INFO)
      
      web_dir=options[:web_dir]
      filename=filename
      s= HTTPServer.new(
        :Port => options[:port],
        :Logger => webrick_logger,
        :AccessLog => webrick_logger
      )
      s.mount("/#{filename}", FileServlet,File.join(web_dir,filename))
      trap("INT"){
          s.shutdown
          puts "Stopping webserver"
          exit
        }
      s.start
    end
    
  end
end
Something went wrong with that request. Please try again.