mod_echo.c revision 3f4d7fdb598d64d80ab8656852276246f2f3b502
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* ====================================================================
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * The Apache Software License, Version 1.1
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * reserved.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Redistribution and use in source and binary forms, with or without
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * modification, are permitted provided that the following conditions
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * are met:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 1. Redistributions of source code must retain the above copyright
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * notice, this list of conditions and the following disclaimer.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 2. Redistributions in binary form must reproduce the above copyright
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * notice, this list of conditions and the following disclaimer in
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the documentation and/or other materials provided with the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * distribution.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 3. The end-user documentation included with the redistribution,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * if any, must include the following acknowledgment:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * "This product includes software developed by the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Apache Software Foundation (http://www.apache.org/)."
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Alternately, this acknowledgment may appear in the software itself,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * if and wherever such third-party acknowledgments normally appear.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 4. The names "Apache" and "Apache Software Foundation" must
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * not be used to endorse or promote products derived from this
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * software without prior written permission. For written
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * permission, please contact apache@apache.org.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 5. Products derived from this software may not be called "Apache",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * nor may "Apache" appear in their name, without prior written
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * permission of the Apache Software Foundation.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * SUCH DAMAGE.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ====================================================================
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * This software consists of voluntary contributions made by many
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * individuals on behalf of the Apache Software Foundation. For more
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * information on the Apache Software Foundation, please see
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * <http://www.apache.org/>.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Portions of this software are based upon public domain software
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * originally written at the National Center for Supercomputing Applications,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * University of Illinois, Urbana-Champaign.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "ap_config.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "ap_mmn.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "httpd.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "http_config.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "http_connection.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "apr_buckets.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "util_filter.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingAP_DECLARE_DATA module echo_module;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingtypedef struct {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int bEnabled;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding} EchoConfig;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic void *create_echo_server_config(apr_pool_t *p, server_rec *s)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding EchoConfig *pConfig = apr_pcalloc(p, sizeof *pConfig);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pConfig->bEnabled = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return pConfig;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic const char *echo_on(cmd_parms *cmd, void *dummy, int arg)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding EchoConfig *pConfig = ap_get_module_config(cmd->server->module_config,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding &echo_module);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pConfig->bEnabled = arg;
ab5581cc78e9d865b0a6ab1404c53347b3276968rbb
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbbstatic int process_echo_connection(conn_rec *c)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_bucket_brigade *bb;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_bucket *b;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_status_t rv;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int zero = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding EchoConfig *pConfig = ap_get_module_config(c->base_server->module_config,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding &echo_module);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!pConfig->bEnabled) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return DECLINED;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding bb = apr_brigade_create(c->pool);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for ( ; ; ) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Get a single line of input from the client */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ((rv = ap_get_brigade(c->input_filters, bb,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding AP_MODE_BLOCKING, &zero) != APR_SUCCESS ||
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding APR_BRIGADE_EMPTY(bb))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_brigade_destroy(bb);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding break;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Make sure the data is flushed to the client */
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbb b = apr_bucket_flush_create();
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding APR_BRIGADE_INSERT_TAIL(bb, b);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_pass_brigade(c->output_filters, bb);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return OK;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic const command_rec echo_cmds[] =
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding AP_INIT_FLAG("ProtocolEcho", echo_on, NULL, RSRC_CONF,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Run an echo server on this host"),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding { NULL }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding};
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic void register_hooks(apr_pool_t *p)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_hook_process_connection(process_echo_connection, NULL, NULL,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding APR_HOOK_MIDDLE);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingAP_DECLARE_DATA module echo_module = {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding STANDARD20_MODULE_STUFF,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding NULL, /* create per-directory config structure */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding NULL, /* merge per-directory config structures */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding create_echo_server_config, /* create per-server config structure */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding NULL, /* merge per-server config structures */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding echo_cmds, /* command apr_table_t */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding register_hooks /* register hooks */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding};
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding