""" 03_mod_echo.py: A module illustrating how to use the pre and process connection hooks in Apache 2.0. This module will read input from the remote client, and echo it back. The "ModEcho" directive will turn on the echo protocol for whichever virtualserver the directive is used within. *CONCEPTS: - Protocol extension *WARNING: This module only works within Apache 2 because the connection hook facilities do not exist in Apache 1.3 *TO USE: Insert the following lines in your httpd.conf file: ---- SnakeModuleDir /path/to/mod_snake/examples/tut SnakeModule 03_mod_echo.Mod_Echo ---- where /path/to/mod_snake is the path to the extracted mod_snake source. Add the following directive within either a VirtualHost block or within the main config file: ModEcho on Then simply telnet to whichever port the virtualhost is running on and play with the module. """ import mod_snake import sys class Mod_Echo: def __init__(self, module): directives = { "ModEcho" : (mod_snake.RSRC_CONF, mod_snake.FLAG, self.cmd_ModEcho)} module.add_directives(directives) # The process_connection hook is the second hook ever run when # processing an incoming connection. The first is pre_connection. # This is the facility which allows us to handle the connection # in whatever manner we wish. module.add_hook('process_connection', self.process_connection) module.add_hook('create_svr_config', self.create_svr_config) def create_svr_config(self, svr): return { 'server' : svr, 'enabled' : 0 } def cmd_ModEcho(self, dir_cfg, svr_cfg, on_or_off): svr_cfg['enabled'] = on_or_off def process_connection(self, svr_cfg, conn): if not svr_cfg['enabled']: return mod_snake.DECLINED # The 'conn' object gives us a nice interface for reading and writing # to the remote client. # Give them a nice friendly greeting. conn.write("Hello, I'm mod_echo --- Let's chat!\n") conn.flush() backfire = '' # This should probably be re-written to not do single byte reads while 1: (status, bytes, data) = conn.read(1) if status or not bytes: break backfire = backfire + data if data[0] == '\n': conn.write(backfire) conn.flush() backfire = '' return mod_snake.OK