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