Main Page | Modules | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages | Examples

mod_echo.c

Go to the documentation of this file.
00001 /* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
00002  * applicable.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "ap_config.h"
00018 #include "ap_mmn.h"
00019 #include "httpd.h"
00020 #include "http_config.h"
00021 #include "http_connection.h"
00022 
00023 #include "apr_buckets.h"
00024 #include "util_filter.h"
00025 
00026 module AP_MODULE_DECLARE_DATA echo_module;
00027 
00028 typedef struct {
00029     int bEnabled;
00030 } EchoConfig;
00031 
00032 static void *create_echo_server_config(apr_pool_t *p, server_rec *s)
00033 {
00034     EchoConfig *pConfig = apr_pcalloc(p, sizeof *pConfig);
00035 
00036     pConfig->bEnabled = 0;
00037 
00038     return pConfig;
00039 }
00040 
00041 static const char *echo_on(cmd_parms *cmd, void *dummy, int arg)
00042 {
00043     EchoConfig *pConfig = ap_get_module_config(cmd->server->module_config,
00044                                                &echo_module);
00045     pConfig->bEnabled = arg;
00046 
00047     return NULL;
00048 }
00049 
00050 static int process_echo_connection(conn_rec *c)
00051 {
00052     apr_bucket_brigade *bb;
00053     apr_bucket *b;
00054     apr_status_t rv;
00055     EchoConfig *pConfig = ap_get_module_config(c->base_server->module_config,
00056                                                &echo_module);
00057 
00058     if (!pConfig->bEnabled) {
00059         return DECLINED;
00060     }
00061     
00062     bb = apr_brigade_create(c->pool, c->bucket_alloc);
00063 
00064     for ( ; ; ) {
00065         /* Get a single line of input from the client */
00066         if ((rv = ap_get_brigade(c->input_filters, bb, AP_MODE_GETLINE,
00067                                  APR_BLOCK_READ, 0) != APR_SUCCESS || 
00068              APR_BRIGADE_EMPTY(bb))) {
00069             apr_brigade_destroy(bb);
00070             break;
00071         }
00072 
00073         /* Make sure the data is flushed to the client */
00074         b = apr_bucket_flush_create(c->bucket_alloc);
00075         APR_BRIGADE_INSERT_TAIL(bb, b);
00076         ap_pass_brigade(c->output_filters, bb);    
00077     }
00078     return OK;
00079 }
00080 
00081 static const command_rec echo_cmds[] = 
00082 {
00083     AP_INIT_FLAG("ProtocolEcho", echo_on, NULL, RSRC_CONF,
00084                  "Run an echo server on this host"),
00085     { NULL }
00086 };
00087 
00088 static void register_hooks(apr_pool_t *p)
00089 {
00090     ap_hook_process_connection(process_echo_connection, NULL, NULL,
00091                                APR_HOOK_MIDDLE);
00092 }
00093 
00094 module AP_MODULE_DECLARE_DATA echo_module = {
00095     STANDARD20_MODULE_STUFF,
00096     NULL,                       /* create per-directory config structure */
00097     NULL,                       /* merge per-directory config structures */
00098     create_echo_server_config,  /* create per-server config structure */
00099     NULL,                       /* merge per-server config structures */
00100     echo_cmds,                  /* command apr_table_t */
00101     register_hooks              /* register hooks */
00102 };