Recent Changes - Search:

A collection of useful facts by Joel Williams. Adelaide denizen. Computer tinkerer. Former terrestrial ecology student of Flinders University. PhD candidate at ANU. Editor for freshmeat.net. Adelaide University Mountain Club member and outdoor enthusiast. Occasional networking go-to guy.

Contact me by email at joel at this domain.

Simple HTTPS Server

I'd forgotten a password, but it was saved in my Safari Autofill thing, which is encrypted. I thought I'd snag it using form submissions. Because the site was HTTPS, I needed to have an appropriate server rather than just sniffing network traffic. I could have modified the Apache config but this seemed more troublesome than writing a program to do it.

Hacked together from a few sources.

  require 'webrick'
  require 'webrick/https'

  OPTIONS = {
    :port        => 443,
    :server_root    => '/Library/WebServer/Documents/'

  }

     server = WEBrick::HTTPServer.new(:Port        => OPTIONS[:port].to_i,
                       :SSLEnable        => true,
                       :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
                       :SSLCertName    => [ [ "CN", WEBrick::Utils::getservername ] ],
                       :DocumentRoot => OPTIONS[:server_root]
                      )

  class PostDumper < WEBrick::HTTPServlet::AbstractServlet

      # Reload file for each request, instantly
      # updating the server with code changes 
      # without needing a restart.

  =begin
      def PostDumper.get_instance( config, *options )
         load __FILE__
         PostDumper.new config, *options
      end
  =end

      # cf. http://www.hiveminds.co.uk/node/244, published under the
      # GNU Free Documentation License, http://www.gnu.org/copyleft/fdl.html

      @@instance = nil
      @@instance_creation_mutex = Mutex.new

      def self.get_instance(config, *options)
         #pp @@instance
         @@instance_creation_mutex.synchronize { 
            instance = instance || self.new(config, *options) }
      end

      def do_GET( request, response )
         response.status = 200
         response['Content-Type'] = "text/plain"
         response.body = dump_request( request )
      end

      def do_POST( request, response )
         response.status = 200
         response['Content-Type'] = "text/plain"
         response.body = dump_request( request )
         response.body << request.body
      end

      def dump_request( request )
         request.request_line << "\r\n" <<
         request.raw_header.join( "" ) << "\r\n"
      end
   end

  server.mount("/foo", PostDumper)

  %w(INT).each do |signal|
     trap(signal) { server.shutdown }
  end

  server.start
Edit - History - Print - Recent Changes - Search
Page last modified on September 07, 2010, at 10:08 PM