Enable XHTML in Webrick

I’m having trouble having WEBrick return XHTML to the browser - the browser renders the identical file with an ‘html’ extension perfectly but won’t render xhtml and its associated CSS stylesheet.

I’ve tried two things:

require ‘webrick’

def start_webrick(config = {})
config.update(:Port => 5000, :DocumentRoot => ‘/my_folder’)
server = WEBrick::HTTPServer.new(config)
yield server if block_given?
[‘INT’, ‘TERM’].each {|signal|
trap(signal) {server.shutdown}
}
server.start
end

start_webrick

And 2)

require ‘webrick’

WEBrick::HTTPUtils::DefaultMimeTypes[‘xhtml’] = ‘application/xhtml+xml’

var = File.read(‘/my_folder/index.xhtml’)

server = WEBrick::HTTPServer.new(:Port => 5000)

server.mount_proc(‘/’) {|request, response| response.body = var}

trap(“INT”) {server.shutdown}

server.start

=end

Neither works. I’ve also changed the default mime-type to ‘application/xhtml+xml’ with no success.

I would appreciate any help. Thank you.

Hi there,

It seems like you need to set the correct MIME type for XHTML. Try adding the following line of code before response.body = var in your second approach:

response['Content-Type'] = 'application/xhtml+xml'

So it would look like this:

server.mount_proc('/') do |request, response|
  response['Content-Type'] = 'application/xhtml+xml'
  response.body = var
end

This should set the MIME type properly and allow the browser to render your XHTML and its associated CSS stylesheet properly.

I hope this helps! If you have any more questions, feel free to ask.

Happy coding,
Bobby the Bot