<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stuporglue.org &#187; webrick</title>
	<atom:link href="http://stuporglue.org/tag/webrick/feed/" rel="self" type="application/rss+xml" />
	<link>http://stuporglue.org</link>
	<description>Programming, Rambling and More!</description>
	<lastBuildDate>Mon, 21 May 2012 05:11:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Ruby Web Server with SSI</title>
		<link>http://stuporglue.org/ruby-web-server-with-ssi/</link>
		<comments>http://stuporglue.org/ruby-web-server-with-ssi/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 13:47:36 +0000</pubDate>
		<dc:creator>stuporglue</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[web server]]></category>
		<category><![CDATA[webrick]]></category>

		<guid isPermaLink="false">http://stuporglue.org/?p=14</guid>
		<description><![CDATA[For a class I had to write a threaded webserver that would insert a banner at the top of each page (bonus if it was randomly selected) and some dynamic content at the bottom. I chose to do it in Ruby, and was rewarded with about 100 lines less of code than the Java version. See the Java version starting point other students used here: http://fragments.turtlemeat.com/javawebserver.php. It could be a little cleaner, but it's not too bad.  <a href="http://stuporglue.org/ruby-web-server-with-ssi/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For a class I had to write a threaded webserver that would insert a banner at the top of each page (bonus if it was randomly selected) and some dynamic content at the bottom. I chose to do it in Ruby, and was rewarded with about 100 lines less of code than the Java version. See the Java version starting point other students used here: <a href="http://fragments.turtlemeat.com/javawebserver.php" target="_BLANK">http://fragments.turtlemeat.com/javawebserver.php</a>. It could be a little cleaner, but it&#8217;s not too bad.</p>
<p><span style="color: #ff0000;">This page is OLD and probably contains errors, out of date information, security flaws or other problems. I am keeping it around because it might be helpful to someone.</span></p>
<pre>#!/usr/bin/env ruby
    # Michael Moore, IT 210, November 2006,
    # Simple web server that does SSI and random banner rotation

    require 'webrick' # require needed libraries (included in ruby)

    $docroot = "j://rws/docs/"  # Set document root as a global variable
    server = WEBrick::HTTPServer.new(:Port =&gt; 8888,:DocumentRoot =&gt; $docroot)  # new HTTP server on localhost:8888

    class SuperServlet  &lt; WEBrick::HTTPServlet::AbstractServlet # override default HTTP GET handler
	def do_GET(req, res)  # req == HTTP request object, res == HTTP response object

	filename = req.meta_vars["PATH_INFO"].gsub(req.meta_vars["QUERY_STRING"],"") # Get just the requested filename

	    if filename.downcase.slice(filename.length - 4,4).slice('html') # if it ends in html, return a modded version
		# Read file into a single string, do some search and replaces (gsub) add a random banner (0.JPG--4.JPG) and return it to the requester
		res.body = IO.readlines($docroot + filename).join.gsub("&lt;body&gt;","&lt;body&gt;&lt;img src='/#{rand(5)}.JPG'&gt;&lt;br&gt;").gsub("&lt;/body&gt;","&lt;br&gt;#{Time.now.to_s}&lt;/body&gt;")
	    else # It wasn't an HTML page, so just return the requested file
		res['content-length'] = File::stat($docroot + filename).size # without this, the browser doesn't know when to quit?
		res.body = open($docroot + filename, "rb") # return the file contents
	    end
	end
    end
    server.mount("/", SuperServlet)  # Use our custom override on anything above this point (ie. all dirs)
    trap("INT"){server.shutdown}     # Catch ctrl-c to do a clean shutdown
    server.starting # Start the server!</pre>
]]></content:encoded>
			<wfw:commentRss>http://stuporglue.org/ruby-web-server-with-ssi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

