mod_echo.c revision ebe70c2684539a5fb2d899241d1601710dfa38a4
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl/* ====================================================================
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl * The Apache Software License, Version 1.1
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl *
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl * Copyright (c) 2000 The Apache Software Foundation. All rights
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl * reserved.
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl *
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl * Redistribution and use in source and binary forms, with or without
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl * modification, are permitted provided that the following conditions
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl * are met:
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl *
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl * 1. Redistributions of source code must retain the above copyright
a530dde7009b0a808300c420def741354a4d13d2Martin Kühl * notice, this list of conditions and the following disclaimer.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco *
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * 2. Redistributions in binary form must reproduce the above copyright
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * notice, this list of conditions and the following disclaimer in
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * the documentation and/or other materials provided with the
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * distribution.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco *
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * 3. The end-user documentation included with the redistribution,
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * if any, must include the following acknowledgment:
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * "This product includes software developed by the
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * Apache Software Foundation (http://www.apache.org/)."
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * Alternately, this acknowledgment may appear in the software itself,
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * if and wherever such third-party acknowledgments normally appear.
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco *
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * 4. The names "Apache" and "Apache Software Foundation" must
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * not be used to endorse or promote products derived from this
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * software without prior written permission. For written
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * permission, please contact apache@apache.org.
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco *
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * 5. Products derived from this software may not be called "Apache",
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * nor may "Apache" appear in their name, without prior written
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * permission of the Apache Software Foundation.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco *
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
b9840e4ee6fda6e42fa4ee9f337482ccc4839a39Adrián Riesco * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
fecce42517d20490f893c4a9dee29b000e1653eaAdrián Riesco * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * SUCH DAMAGE.
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * ====================================================================
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco *
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * This software consists of voluntary contributions made by many
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * individuals on behalf of the Apache Software Foundation. For more
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * information on the Apache Software Foundation, please see
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * <http://www.apache.org/>.
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco *
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * Portions of this software are based upon public domain software
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * originally written at the National Center for Supercomputing Applications,
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco * University of Illinois, Urbana-Champaign.
aea9000fc94442cbfc92596f4264473c0fce51e4Adrián Riesco */
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco#include "ap_config.h"
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco#include "ap_mmn.h"
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco#include "httpd.h"
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco#include "http_config.h"
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco#include "http_connection.h"
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián RiescoAPI_VAR_EXPORT module echo_module;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riescotypedef struct
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco {
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco int bEnabled;
7474965b2e6323002c96c0b39a59843cde201870Adrián Riesco } EchoConfig;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riescostatic void *create_echo_server_config(ap_pool_t *p,server_rec *s)
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco {
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco EchoConfig *pConfig=ap_pcalloc(p,sizeof *pConfig);
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco pConfig->bEnabled=0;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco return pConfig;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco }
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riescostatic const char *echo_on(cmd_parms *cmd, void *dummy, const char *arg)
7474965b2e6323002c96c0b39a59843cde201870Adrián Riesco {
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco EchoConfig *pConfig=ap_get_module_config(cmd->server->module_config,
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco &echo_module);
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco pConfig->bEnabled=1;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco return NULL;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco }
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riescostatic int process_echo_connection(conn_rec *c)
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco {
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco char buf[1024];
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco EchoConfig *pConfig=ap_get_module_config(c->base_server->module_config,
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco &echo_module);
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
7474965b2e6323002c96c0b39a59843cde201870Adrián Riesco if(!pConfig->bEnabled)
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco return DECLINED;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco for( ; ; )
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco {
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco ap_ssize_t r, w;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco (void) ap_bread(c->client,buf,sizeof buf,&r);
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco if(r <= 0)
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco break;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco (void) ap_bwrite(c->client,buf,r, &w);
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco if(w != r)
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco break;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco ap_bflush(c->client);
7474965b2e6323002c96c0b39a59843cde201870Adrián Riesco }
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco return OK;
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco }
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riescostatic const command_rec echo_cmds[] =
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco{
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco AP_INIT_RAW_ARGS("ProtocolEcho", echo_on, NULL, RSRC_CONF,
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco "Run an echo server on this host"),
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco { NULL }
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco};
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riescostatic void register_hooks(void)
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco{
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco ap_hook_process_connection(process_echo_connection,NULL,NULL,AP_HOOK_MIDDLE);
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco}
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián RiescoAPI_VAR_EXPORT module echo_module = {
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco STANDARD20_MODULE_STUFF,
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco NULL, /* create per-directory config structure */
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco NULL, /* merge per-directory config structures */
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco create_echo_server_config, /* create per-server config structure */
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco NULL, /* merge per-server config structures */
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco echo_cmds, /* command ap_table_t */
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco NULL, /* handlers */
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco register_hooks /* register hooks */
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco};
5318901bb69bf247e0f341312c800ba4ea87e46bAdrián Riesco