mod_echo.c revision b931e2cf90d43b0894c2e2927a2a42aca46f277e
3852N/A/* Licensed to the Apache Software Foundation (ASF) under one or more
3852N/A * contributor license agreements. See the NOTICE file distributed with
3852N/A * this work for additional information regarding copyright ownership.
3852N/A * The ASF licenses this file to You under the Apache License, Version 2.0
3852N/A * (the "License"); you may not use this file except in compliance with
3852N/A * the License. You may obtain a copy of the License at
3852N/A *
3852N/A * http://www.apache.org/licenses/LICENSE-2.0
3852N/A *
3852N/A * Unless required by applicable law or agreed to in writing, software
3852N/A * distributed under the License is distributed on an "AS IS" BASIS,
3852N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3852N/A * See the License for the specific language governing permissions and
3852N/A * limitations under the License.
3852N/A */
3852N/A
3852N/A#include "ap_config.h"
3852N/A#include "ap_mmn.h"
3852N/A#include "httpd.h"
3852N/A#include "http_config.h"
3852N/A#include "http_connection.h"
3852N/A
3852N/A#include "apr_buckets.h"
3852N/A#include "util_filter.h"
3852N/A
3852N/Amodule AP_MODULE_DECLARE_DATA echo_module;
3852N/A
3852N/Atypedef struct {
3852N/A int bEnabled;
3852N/A} EchoConfig;
3852N/A
3852N/Astatic void *create_echo_server_config(apr_pool_t *p, server_rec *s)
3852N/A{
3852N/A EchoConfig *pConfig = apr_pcalloc(p, sizeof *pConfig);
3852N/A
3852N/A pConfig->bEnabled = 0;
3852N/A
3852N/A return pConfig;
3852N/A}
3852N/A
3852N/Astatic const char *echo_on(cmd_parms *cmd, void *dummy, int arg)
3852N/A{
3852N/A EchoConfig *pConfig = ap_get_module_config(cmd->server->module_config,
3852N/A &echo_module);
3852N/A pConfig->bEnabled = arg;
3852N/A
3852N/A return NULL;
3852N/A}
3852N/A
3852N/Astatic int process_echo_connection(conn_rec *c)
3852N/A{
3852N/A apr_bucket_brigade *bb;
3852N/A apr_bucket *b;
3852N/A apr_status_t rv;
3852N/A EchoConfig *pConfig = ap_get_module_config(c->base_server->module_config,
3852N/A &echo_module);
3852N/A
3852N/A if (!pConfig->bEnabled) {
3852N/A return DECLINED;
3852N/A }
3852N/A
3852N/A do {
3852N/A bb = apr_brigade_create(c->pool, c->bucket_alloc);
3852N/A
3852N/A /* Get a single line of input from the client */
3852N/A if (((rv = ap_get_brigade(c->input_filters, bb, AP_MODE_GETLINE,
3852N/A APR_BLOCK_READ, 0)) != APR_SUCCESS) ||
3852N/A APR_BRIGADE_EMPTY(bb)) {
3852N/A apr_brigade_destroy(bb);
3852N/A break;
3852N/A }
3852N/A
3852N/A /* Make sure the data is flushed to the client */
3852N/A b = apr_bucket_flush_create(c->bucket_alloc);
3852N/A APR_BRIGADE_INSERT_TAIL(bb, b);
3852N/A
3852N/A /* Send back the data. */
3852N/A rv = ap_pass_brigade(c->output_filters, bb);
3852N/A } while (rv == APR_SUCCESS);
3852N/A
3852N/A return OK;
3852N/A}
3852N/A
3852N/Astatic const command_rec echo_cmds[] =
3852N/A{
3852N/A AP_INIT_FLAG("ProtocolEcho", echo_on, NULL, RSRC_CONF,
3852N/A "Run an echo server on this host"),
3852N/A { NULL }
};
static void register_hooks(apr_pool_t *p)
{
ap_hook_process_connection(process_echo_connection, NULL, NULL,
APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA echo_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
create_echo_server_config, /* create per-server config structure */
NULL, /* merge per-server config structures */
echo_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};