mod_echo.c revision 7619b51fca6ecf16c28389e52a666e82f3c8d3ef
6ae232055d4d8a97267517c5e50074c2c819941and/* Licensed to the Apache Software Foundation (ASF) under one or more
6ae232055d4d8a97267517c5e50074c2c819941and * contributor license agreements. See the NOTICE file distributed with
6ae232055d4d8a97267517c5e50074c2c819941and * this work for additional information regarding copyright ownership.
6ae232055d4d8a97267517c5e50074c2c819941and * The ASF licenses this file to You under the Apache License, Version 2.0
6ae232055d4d8a97267517c5e50074c2c819941and * (the "License"); you may not use this file except in compliance with
6ae232055d4d8a97267517c5e50074c2c819941and * the License. You may obtain a copy of the License at
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * http://www.apache.org/licenses/LICENSE-2.0
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Unless required by applicable law or agreed to in writing, software
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * distributed under the License is distributed on an "AS IS" BASIS,
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * See the License for the specific language governing permissions and
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * limitations under the License.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include "ap_config.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "ap_mmn.h"
3f08db06526d6901aa08c110b5bc7dde6bc39905nd#include "httpd.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_config.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_connection.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_core.h"
b43f840409794ed298e8634f6284741f193b6c4ftakashi#include "http_log.h"
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_buckets.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_strings.h"
b43f840409794ed298e8634f6284741f193b6c4ftakashi#include "util_filter.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "scoreboard.h"
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung
6ae232055d4d8a97267517c5e50074c2c819941andmodule AP_MODULE_DECLARE_DATA echo_module;
b43f840409794ed298e8634f6284741f193b6c4ftakashi
b43f840409794ed298e8634f6284741f193b6c4ftakashitypedef struct {
b43f840409794ed298e8634f6284741f193b6c4ftakashi int bEnabled;
b43f840409794ed298e8634f6284741f193b6c4ftakashi} EchoConfig;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic void *create_echo_server_config(apr_pool_t *p, server_rec *s)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and EchoConfig *pConfig = apr_pcalloc(p, sizeof *pConfig);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and pConfig->bEnabled = 0;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and return pConfig;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic const char *echo_on(cmd_parms *cmd, void *dummy, int arg)
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi{
6ae232055d4d8a97267517c5e50074c2c819941and EchoConfig *pConfig = ap_get_module_config(cmd->server->module_config,
6ae232055d4d8a97267517c5e50074c2c819941and &echo_module);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi pConfig->bEnabled = arg;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and return NULL;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic apr_status_t brigade_peek(apr_bucket_brigade *bbIn,
6ae232055d4d8a97267517c5e50074c2c819941and char *buff, apr_size_t bufflen)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and apr_bucket *b;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi apr_size_t readbytes = 0;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi if (bufflen--)
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi /* compensate for NULL */
6ae232055d4d8a97267517c5e50074c2c819941and *buff = '\0';
6ae232055d4d8a97267517c5e50074c2c819941and else
6ae232055d4d8a97267517c5e50074c2c819941and return APR_EGENERAL;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if (APR_BRIGADE_EMPTY(bbIn))
6ae232055d4d8a97267517c5e50074c2c819941and return APR_EGENERAL;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and b = APR_BRIGADE_FIRST(bbIn);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and while ((b != APR_BRIGADE_SENTINEL(bbIn)) && (readbytes < bufflen)) {
6ae232055d4d8a97267517c5e50074c2c819941and const char *pos;
6ae232055d4d8a97267517c5e50074c2c819941and const char *str;
6ae232055d4d8a97267517c5e50074c2c819941and apr_size_t len;
6ae232055d4d8a97267517c5e50074c2c819941and apr_status_t rv;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if ((rv = apr_bucket_read(b, &str, &len, APR_NONBLOCK_READ))
6ae232055d4d8a97267517c5e50074c2c819941and != APR_SUCCESS)
6ae232055d4d8a97267517c5e50074c2c819941and return rv;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if ((pos = memchr(str, APR_ASCII_LF, len)) != NULL)
6ae232055d4d8a97267517c5e50074c2c819941and len = pos - str;
6ae232055d4d8a97267517c5e50074c2c819941and if (len > bufflen - readbytes)
6ae232055d4d8a97267517c5e50074c2c819941and len = bufflen - readbytes;
6ae232055d4d8a97267517c5e50074c2c819941and memcpy (buff + readbytes, str, len);
6ae232055d4d8a97267517c5e50074c2c819941and readbytes += len;
6ae232055d4d8a97267517c5e50074c2c819941and buff[readbytes] = '\0';
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and b = APR_BUCKET_NEXT(b);
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and return APR_SUCCESS;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic int update_echo_child_status(ap_sb_handle_t *sbh,
6ae232055d4d8a97267517c5e50074c2c819941and int status, conn_rec *c,
6ae232055d4d8a97267517c5e50074c2c819941and apr_bucket_brigade *last_echoed)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and worker_score *ws = ap_get_scoreboard_worker(sbh);
6ae232055d4d8a97267517c5e50074c2c819941and int old_status = ws->status;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and ws->status = status;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if (!ap_extended_status)
6ae232055d4d8a97267517c5e50074c2c819941and return old_status;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and ws->last_used = apr_time_now();
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* initial pass only, please - in the name of efficiency */
6ae232055d4d8a97267517c5e50074c2c819941and if (c) {
6ae232055d4d8a97267517c5e50074c2c819941and apr_cpystrn(ws->client,
6ae232055d4d8a97267517c5e50074c2c819941and ap_get_remote_host(c, c->base_server->lookup_defaults,
6ae232055d4d8a97267517c5e50074c2c819941and REMOTE_NOLOOKUP, NULL),
6ae232055d4d8a97267517c5e50074c2c819941and sizeof(ws->client));
6ae232055d4d8a97267517c5e50074c2c819941and apr_cpystrn(ws->vhost, c->base_server->server_hostname,
6ae232055d4d8a97267517c5e50074c2c819941and sizeof(ws->vhost));
6ae232055d4d8a97267517c5e50074c2c819941and /* Deliberate trailing space - filling in string on WRITE passes */
6ae232055d4d8a97267517c5e50074c2c819941and apr_cpystrn(ws->request, "ECHO ", sizeof(ws->request));
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* each subsequent WRITE pass, let's update what we echoed */
6ae232055d4d8a97267517c5e50074c2c819941and if (last_echoed) {
6ae232055d4d8a97267517c5e50074c2c819941and brigade_peek(last_echoed, ws->request + sizeof("ECHO ") - 1,
6ae232055d4d8a97267517c5e50074c2c819941and sizeof(ws->request) - sizeof("ECHO ") + 1);
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and return old_status;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic int process_echo_connection(conn_rec *c)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and apr_bucket_brigade *bb;
6ae232055d4d8a97267517c5e50074c2c819941and apr_bucket *b;
6ae232055d4d8a97267517c5e50074c2c819941and apr_socket_t *csd = NULL;
6ae232055d4d8a97267517c5e50074c2c819941and EchoConfig *pConfig = ap_get_module_config(c->base_server->module_config,
6ae232055d4d8a97267517c5e50074c2c819941and &echo_module);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if (!pConfig->bEnabled) {
6ae232055d4d8a97267517c5e50074c2c819941and return DECLINED;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and ap_time_process_request(c->sbh, START_PREQUEST);
6ae232055d4d8a97267517c5e50074c2c819941and update_echo_child_status(c->sbh, SERVER_BUSY_READ, c, NULL);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and bb = apr_brigade_create(c->pool, c->bucket_alloc);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and for ( ; ; ) {
6ae232055d4d8a97267517c5e50074c2c819941and apr_status_t rv;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* Get a single line of input from the client */
6ae232055d4d8a97267517c5e50074c2c819941and if (((rv = ap_get_brigade(c->input_filters, bb, AP_MODE_GETLINE,
6ae232055d4d8a97267517c5e50074c2c819941and APR_BLOCK_READ, 0)) != APR_SUCCESS)) {
6ae232055d4d8a97267517c5e50074c2c819941and apr_brigade_cleanup(bb);
6ae232055d4d8a97267517c5e50074c2c819941and if (!APR_STATUS_IS_EOF(rv) && ! APR_STATUS_IS_TIMEUP(rv))
6ae232055d4d8a97267517c5e50074c2c819941and ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server, APLOGNO(01611)
6ae232055d4d8a97267517c5e50074c2c819941and "ProtocolEcho: Failure reading from %s",
6ae232055d4d8a97267517c5e50074c2c819941and c->client_ip);
6ae232055d4d8a97267517c5e50074c2c819941and break;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* Something horribly wrong happened. Someone didn't block! */
6ae232055d4d8a97267517c5e50074c2c819941and if (APR_BRIGADE_EMPTY(bb)) {
6ae232055d4d8a97267517c5e50074c2c819941and ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server, APLOGNO(01612)
6ae232055d4d8a97267517c5e50074c2c819941and "ProtocolEcho: Error - read empty brigade from %s!",
6ae232055d4d8a97267517c5e50074c2c819941and c->client_ip);
6ae232055d4d8a97267517c5e50074c2c819941and break;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if (!csd) {
6ae232055d4d8a97267517c5e50074c2c819941and csd = ap_get_conn_socket(c);
6ae232055d4d8a97267517c5e50074c2c819941and apr_socket_timeout_set(csd, c->base_server->keep_alive_timeout);
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and update_echo_child_status(c->sbh, SERVER_BUSY_WRITE, NULL, bb);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* Make sure the data is flushed to the client */
6ae232055d4d8a97267517c5e50074c2c819941and b = apr_bucket_flush_create(c->bucket_alloc);
6ae232055d4d8a97267517c5e50074c2c819941and APR_BRIGADE_INSERT_TAIL(bb, b);
6ae232055d4d8a97267517c5e50074c2c819941and rv = ap_pass_brigade(c->output_filters, bb);
6ae232055d4d8a97267517c5e50074c2c819941and if (rv != APR_SUCCESS) {
6ae232055d4d8a97267517c5e50074c2c819941and ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server, APLOGNO(01613)
6ae232055d4d8a97267517c5e50074c2c819941and "ProtocolEcho: Failure writing to %s",
6ae232055d4d8a97267517c5e50074c2c819941and c->client_ip);
6ae232055d4d8a97267517c5e50074c2c819941and break;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and apr_brigade_cleanup(bb);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* Announce our intent to loop */
6ae232055d4d8a97267517c5e50074c2c819941and update_echo_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL, NULL);
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and apr_brigade_destroy(bb);
6ae232055d4d8a97267517c5e50074c2c819941and ap_time_process_request(c->sbh, STOP_PREQUEST);
6ae232055d4d8a97267517c5e50074c2c819941and update_echo_child_status(c->sbh, SERVER_CLOSING, c, NULL);
6ae232055d4d8a97267517c5e50074c2c819941and return OK;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic const command_rec echo_cmds[] =
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and AP_INIT_FLAG("ProtocolEcho", echo_on, NULL, RSRC_CONF,
6ae232055d4d8a97267517c5e50074c2c819941and "Run an echo server on this host"),
6ae232055d4d8a97267517c5e50074c2c819941and { NULL }
6ae232055d4d8a97267517c5e50074c2c819941and};
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic void register_hooks(apr_pool_t *p)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and ap_hook_process_connection(process_echo_connection, NULL, NULL,
6ae232055d4d8a97267517c5e50074c2c819941and APR_HOOK_MIDDLE);
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andAP_DECLARE_MODULE(echo) = {
6ae232055d4d8a97267517c5e50074c2c819941and STANDARD20_MODULE_STUFF,
6ae232055d4d8a97267517c5e50074c2c819941and NULL, /* create per-directory config structure */
6ae232055d4d8a97267517c5e50074c2c819941and NULL, /* merge per-directory config structures */
6ae232055d4d8a97267517c5e50074c2c819941and create_echo_server_config, /* create per-server config structure */
6ae232055d4d8a97267517c5e50074c2c819941and NULL, /* merge per-server config structures */
6ae232055d4d8a97267517c5e50074c2c819941and echo_cmds, /* command apr_table_t */
6ae232055d4d8a97267517c5e50074c2c819941and register_hooks /* register hooks */
6ae232055d4d8a97267517c5e50074c2c819941and};
6ae232055d4d8a97267517c5e50074c2c819941and