server snippets

Simple Mongrel HTTP server and custom Mongrel handler example

Tagged mongrel, http, server, handler  Languages ruby
# gem install -y mongrel
require 'rubygems'
require 'mongrel'

# Usage: ruby mongrel_http_server.rb <host> <port> <docroot>
host    = ARGV[0] || ""
port    = ARGV[1] || 80
docroot = ARGV[2] || "html/"

# Simple Mongrel handler that prints the current date and time
class HandlerExample < Mongrel::HttpHandler
   def process(request, response)
      response.start(200) do |head, out|
         head["Content-Type"] = "text/html"

# Configure Mongrel and handlers
config = :host => host, :port => port do
  listener do
    uri "/",              :handler =>
    uri "/handler_example", :handler =>, :in_front => true

  # CTRL+C to stop server
  trap("INT") { stop }

# Start Mongrel
puts "Mongrel listening on '#{host}:#{port}', serving documents from '#{docroot}'."

How to change the Apache 2 server signature on Debian Etch

Tagged apache2, signature, server, header, disable  Languages apacheconf

Install mod_security, for some stupid reason it's not included in Debian Etch, and for some even more stupid reason you're not allowed to change the value of the Server header.

Anyway, to change the server signature, and enable voodoo magic:

<IfModule mod_security2.c>
    # Basic configuration options
    SecRuleEngine On
    SecRequestBodyAccess On
    SecResponseBodyAccess Off

    # Handling of file uploads
    # TODO Choose a folder private to Apache.
    # SecUploadDir /opt/apache-frontend/tmp/
    SecUploadKeepFiles Off

    # Debug log
    SecDebugLog /var/log/apache2/modsec_debug.log
    SecDebugLogLevel 0

    # Serial audit log
    SecAuditEngine RelevantOnly
    SecAuditLogRelevantStatus ^5
    SecAuditLogParts ABIFHZ
    SecAuditLogType Serial
    SecAuditLog /var/log/apache2/modsec_audit.log

    # Maximum request body size we will
    # accept for buffering
    SecRequestBodyLimit 131072

    # Store up to 128 KB in memory
    SecRequestBodyInMemoryLimit 131072

    # Buffer response bodies of up to
    # 512 KB in length
    SecResponseBodyLimit 524288

    SecServerSignature "Dummy value"

How to hide X-Powered-By and Server headers

Tagged servertokens, server, x-powered-by, mod_rails, passenger, apache, apache2, headers  Languages apacheconf

First enable the mod_headers module:

sudo a2enmod headers

Then add this to your apache2.conf:

# Hide X-Powered-By and Server headers
Header always unset "X-Powered-By"
ServerTokens Prod
ServerSignature Off

Now restart Apache:

/etc/init.d/apache2 force-reload

This is security through obscurity at it's finest...

Eventmachine Echo Server Example

Tagged eventmachine, echo, server, tcp  Languages ruby
require 'rubygems'
require 'eventmachine'
require 'logger'

# Each connection creates a new EchoServer.
module EchoServer
  LOG ='echo.log')

  # Called by the event loop immediately after the network connection has been
  # established, and before resumption of the network loop.
  def post_init "-- Connection established #{remote_ip} --"

  # Called by the event loop whenever data has been received by the network
  # connection. It is never called by user code.
  def receive_data data
    message = "#{remote_ip}: #{data}" message
    send_data message
    close_connection if data =~ /quit/i

  # Called by the framework whenever a connection (either a server or client
  # connection) is closed.
  def unbind "-- Connection closed #{remote_ip} --"

  # Return the IP and port of the remote client.
  def remote_ip
    @remote_ip ||= begin
                     port, ip = Socket.unpack_sockaddr_in(get_peername)

EventMachine::run do
  Signal.trap("INT")  { EventMachine.stop }
  Signal.trap("TERM") { EventMachine.stop }
  EventMachine::start_server "", 8080, EchoServer

Use telnet to test it:

$ telnet localhost 8080