Forum: Ruby windows and http servers on ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
12e343b1318a33b18f98572be860618b?d=identicon&s=25 Jacek Podkanski (Guest)
on 2006-03-03 23:10
(Received via mailing list)
Hi,
I wrote custom webrick servlet which parses *.html files with embedded
ruby code. It works fine on Linux, but on Windows it doesn't work
properly
because pictures that are supposed to be on a web page do not show.

What can I do?

#!/usr/bin/ruby
require 'webrick'
include WEBrick

class CustomServlet < HTTPServlet::AbstractServlet
	def do_GET(req, resp)
	unless defined?(ERB)
		@logger.warn "#{self.class}: ERB not defined."
		raise HTTPStatus::Forbidden, "ERBHandler cannot work."
	end
	#puts req.inspect
	#puts "++++ #{req.request_uri} +++++++++++++++"
	str=req.inspect
	path=Dir.pwd+req.path
	uri=req.request_uri.to_s
	if File.stat(path).directory?
		resp.body << "\n<html><head><title>Directory</title></head><body>\n"
		resp.body << "<h2>Directory: #{req.path}</h2>"
		entries=Dir.entries(path)
		resp.body << '<a href="/">Root directory</a>'+"<br>\n"
		entries[1..-1].each {|x|
			resp.body << '<a href="' << uri
			resp.body << '/' if req.path[-1,1] != '/'
			resp.body << x << '">'
			x='Parent Directory' if x=='..'
			resp.body << x << "</a><br>\n"
		}
		resp.body << "\n</body></html>"
	else
        #puts 'going to process file '+path
        file=File.open(path).read #load normal file
        if (path[-5..-1]=='.html' or path[-4..-1]=='.htm')
			rhtml=ERB.new(file) #convert served string into code
			#rhtml.run #run the code
			file=rhtml.result #copy result to another string
			resp.body << file
		else
			resp.body << file
		end #if
	end #if

  end#def
end#class



def start_webrick(config = {})
  config.update(:Port => 8080)
  server = HTTPServer.new(config)
  yield server if block_given?
  ['INT', 'TERM'].each {|signal|
    trap(signal) {server.shutdown}
  }
  server.start
end

start_webrick {|server|
server.mount('/',CustomServlet,"#{Dir.pwd}/proba")
}
F3b7b8756d0c7f71cc7460cc33aefaee?d=identicon&s=25 Daniel Berger (Guest)
on 2006-03-03 23:13
(Received via mailing list)
Jacek Podkanski wrote:
> Hi,
> I wrote custom webrick servlet which parses *.html files with embedded
> ruby code. It works fine on Linux, but on Windows it doesn't work properly
> because pictures that are supposed to be on a web page do not show.
>
> What can I do?

<snip>

I don't know if this is your only issue, but there's no "/" (root) on
Windows.
Try a drive letter (e.g. 'c:\').

Regards,

Dan
Baea740a92efb1faa302bf510153e700?d=identicon&s=25 Jacek Podkanski (Guest)
on 2006-03-04 00:14
>
> I don't know if this is your only issue, but there's no "/" (root) on
> Windows.
> Try a drive letter (e.g. 'c:\').

I also tried that, when i wrote own server based on httpserver class. I
thought it was the issue but it wasn't, all i got was pc speaker
beeping.
This topic is locked and can not be replied to.