mod_echo.c revision 5bfaaf573bacb45c1cf290ce85ecc676587e8a64
6de8046f8f7e07cd83895a528df25d977e502c76nd/* Licensed to the Apache Software Foundation (ASF) under one or more
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * contributor license agreements. See the NOTICE file distributed with
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * this work for additional information regarding copyright ownership.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * The ASF licenses this file to You under the Apache License, Version 2.0
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * (the "License"); you may not use this file except in compliance with
2d2eda71267231c2526be701fe655db125852c1ffielding * the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
2d2eda71267231c2526be701fe655db125852c1ffielding * http://www.apache.org/licenses/LICENSE-2.0
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
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
2d2eda71267231c2526be701fe655db125852c1ffielding * limitations under the License.
2d2eda71267231c2526be701fe655db125852c1ffielding */
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding#include "ap_config.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "ap_mmn.h"
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben#include "httpd.h"
0432a26b69eedfb9ca5f34fba590236378a24851ben#include "http_config.h"
0432a26b69eedfb9ca5f34fba590236378a24851ben#include "http_connection.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "http_core.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "http_log.h"
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb#include "apr_buckets.h"
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb#include "apr_strings.h"
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb#include "util_filter.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "scoreboard.h"
30c289e6bc6d28d210b21edd800ab2cfc78a8381wrowe
cccd31fa4a72fe23cc3249c06db181b274a55a69gsteinmodule AP_MODULE_DECLARE_DATA echo_module;
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein
cd39d2139743ca0ef899953c6496dcf99e9c791atrawicktypedef struct {
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein int bEnabled;
b627048681b27fe30f979ba471b523be3a6a22adrbb} EchoConfig;
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein
cccd31fa4a72fe23cc3249c06db181b274a55a69gsteinstatic void *create_echo_server_config(apr_pool_t *p, server_rec *s)
44c46ef733836b32585d135d2d90856e7cfd9929rbb{
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb EchoConfig *pConfig = apr_pcalloc(p, sizeof *pConfig);
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb pConfig->bEnabled = 0;
9cfc48b742c224c1fbc2c26a4119a3266192c7d2wrowe
9cfc48b742c224c1fbc2c26a4119a3266192c7d2wrowe return pConfig;
7c7372abe2484e7fcf81937b93496d1246e5b816gstein}
7c7372abe2484e7fcf81937b93496d1246e5b816gstein
7c7372abe2484e7fcf81937b93496d1246e5b816gsteinstatic const char *echo_on(cmd_parms *cmd, void *dummy, int arg)
7c7372abe2484e7fcf81937b93496d1246e5b816gstein{
7c7372abe2484e7fcf81937b93496d1246e5b816gstein EchoConfig *pConfig = ap_get_module_config(cmd->server->module_config,
7c7372abe2484e7fcf81937b93496d1246e5b816gstein &echo_module);
7c7372abe2484e7fcf81937b93496d1246e5b816gstein pConfig->bEnabled = arg;
7c7372abe2484e7fcf81937b93496d1246e5b816gstein
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return NULL;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic apr_status_t brigade_peek(apr_bucket_brigade *bbIn,
2d2eda71267231c2526be701fe655db125852c1ffielding char *buff, apr_size_t bufflen)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding apr_bucket *b;
2d2eda71267231c2526be701fe655db125852c1ffielding apr_size_t readbytes = 0;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (bufflen--)
2d2eda71267231c2526be701fe655db125852c1ffielding /* compensate for NULL */
2d2eda71267231c2526be701fe655db125852c1ffielding *buff = '\0';
2d2eda71267231c2526be701fe655db125852c1ffielding else
2d2eda71267231c2526be701fe655db125852c1ffielding return APR_EGENERAL;
2e123e8beedc9f921448c113e2d6823a92fd5261fielding
2e123e8beedc9f921448c113e2d6823a92fd5261fielding if (APR_BRIGADE_EMPTY(bbIn))
2d2eda71267231c2526be701fe655db125852c1ffielding return APR_EGENERAL;
dd5cbadf2df719db2f3c769d03ec847da25854e6bnicholes
dd5cbadf2df719db2f3c769d03ec847da25854e6bnicholes b = APR_BRIGADE_FIRST(bbIn);
dd5cbadf2df719db2f3c769d03ec847da25854e6bnicholes
2d2eda71267231c2526be701fe655db125852c1ffielding while ((b != APR_BRIGADE_SENTINEL(bbIn)) && (readbytes < bufflen)) {
2d2eda71267231c2526be701fe655db125852c1ffielding const char *pos;
2d2eda71267231c2526be701fe655db125852c1ffielding const char *str;
2d2eda71267231c2526be701fe655db125852c1ffielding apr_size_t len;
2d2eda71267231c2526be701fe655db125852c1ffielding apr_status_t rv;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if ((rv = apr_bucket_read(b, &str, &len, APR_NONBLOCK_READ))
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb != APR_SUCCESS)
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return rv;
72a4ef8eac1adef882246c5bfb9b8bbd82d613c4coar
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if ((pos = memchr(str, APR_ASCII_LF, len)) != NULL)
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb len = pos - str;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (len > bufflen - readbytes)
2d2eda71267231c2526be701fe655db125852c1ffielding len = bufflen - readbytes;
2d2eda71267231c2526be701fe655db125852c1ffielding memcpy (buff + readbytes, str, len);
2d2eda71267231c2526be701fe655db125852c1ffielding readbytes += len;
2d2eda71267231c2526be701fe655db125852c1ffielding buff[readbytes] = '\0';
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding b = APR_BUCKET_NEXT(b);
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding return APR_SUCCESS;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic int update_echo_child_status(ap_sb_handle_t *sbh,
57edbe3cb9356a0b599c7b07f3aae0e721ee57e2coar int status, conn_rec *c,
2d2eda71267231c2526be701fe655db125852c1ffielding apr_bucket_brigade *last_echoed)
a77c6787a1ea7dd88cb6cf8e4ad968b235af2a20jorton{
2d2eda71267231c2526be701fe655db125852c1ffielding worker_score *ws = ap_get_scoreboard_worker(sbh);
2d2eda71267231c2526be701fe655db125852c1ffielding int old_status = ws->status;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding ws->status = status;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (!ap_extended_status)
2d2eda71267231c2526be701fe655db125852c1ffielding return old_status;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding ws->last_used = apr_time_now();
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* initial pass only, please - in the name of efficiency */
2d2eda71267231c2526be701fe655db125852c1ffielding if (c) {
2d2eda71267231c2526be701fe655db125852c1ffielding apr_cpystrn(ws->client,
2d2eda71267231c2526be701fe655db125852c1ffielding ap_get_remote_host(c, c->base_server->lookup_defaults,
2d2eda71267231c2526be701fe655db125852c1ffielding REMOTE_NOLOOKUP, NULL),
2d2eda71267231c2526be701fe655db125852c1ffielding sizeof(ws->client));
2d2eda71267231c2526be701fe655db125852c1ffielding apr_cpystrn(ws->vhost, c->base_server->server_hostname,
2d2eda71267231c2526be701fe655db125852c1ffielding sizeof(ws->vhost));
2d2eda71267231c2526be701fe655db125852c1ffielding /* Deliberate trailing space - filling in string on WRITE passes */
2d2eda71267231c2526be701fe655db125852c1ffielding apr_cpystrn(ws->request, "ECHO ", sizeof(ws->request));
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* each subsequent WRITE pass, let's update what we echoed */
2d2eda71267231c2526be701fe655db125852c1ffielding if (last_echoed) {
2d2eda71267231c2526be701fe655db125852c1ffielding brigade_peek(last_echoed, ws->request + sizeof("ECHO ") - 1,
2d2eda71267231c2526be701fe655db125852c1ffielding sizeof(ws->request) - sizeof("ECHO ") + 1);
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding return old_status;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic int process_echo_connection(conn_rec *c)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding apr_bucket_brigade *bb;
2d2eda71267231c2526be701fe655db125852c1ffielding apr_bucket *b;
2d2eda71267231c2526be701fe655db125852c1ffielding apr_socket_t *csd = NULL;
dbbf1b4183ae16353011a5269b37899f02b97d81gregames EchoConfig *pConfig = ap_get_module_config(c->base_server->module_config,
2d2eda71267231c2526be701fe655db125852c1ffielding &echo_module);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (!pConfig->bEnabled) {
2d2eda71267231c2526be701fe655db125852c1ffielding return DECLINED;
e44e11f9fece12c783f18d033923bfc0d6b4289aake }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding ap_time_process_request(c->sbh, START_PREQUEST);
2d2eda71267231c2526be701fe655db125852c1ffielding update_echo_child_status(c->sbh, SERVER_BUSY_READ, c, NULL);
2d2eda71267231c2526be701fe655db125852c1ffielding
e44e11f9fece12c783f18d033923bfc0d6b4289aake bb = apr_brigade_create(c->pool, c->bucket_alloc);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding for ( ; ; ) {
2d2eda71267231c2526be701fe655db125852c1ffielding apr_status_t rv;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* Get a single line of input from the client */
2d2eda71267231c2526be701fe655db125852c1ffielding if (((rv = ap_get_brigade(c->input_filters, bb, AP_MODE_GETLINE,
2d2eda71267231c2526be701fe655db125852c1ffielding APR_BLOCK_READ, 0)) != APR_SUCCESS)) {
2d2eda71267231c2526be701fe655db125852c1ffielding apr_brigade_cleanup(bb);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!APR_STATUS_IS_EOF(rv) && ! APR_STATUS_IS_TIMEUP(rv))
2d2eda71267231c2526be701fe655db125852c1ffielding ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server,
2d2eda71267231c2526be701fe655db125852c1ffielding "ProtocolEcho: Failure reading from %s",
2d2eda71267231c2526be701fe655db125852c1ffielding c->remote_ip);
2d2eda71267231c2526be701fe655db125852c1ffielding break;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* Something horribly wrong happened. Someone didn't block! */
2d2eda71267231c2526be701fe655db125852c1ffielding if (APR_BRIGADE_EMPTY(bb)) {
2d2eda71267231c2526be701fe655db125852c1ffielding apr_brigade_cleanup(bb);
2d2eda71267231c2526be701fe655db125852c1ffielding ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server,
2d2eda71267231c2526be701fe655db125852c1ffielding "ProtocolEcho: Error - read empty brigade from %s!",
2d2eda71267231c2526be701fe655db125852c1ffielding c->remote_ip);
2d2eda71267231c2526be701fe655db125852c1ffielding break;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (!csd) {
2d2eda71267231c2526be701fe655db125852c1ffielding csd = ap_get_conn_socket(c);
2d2eda71267231c2526be701fe655db125852c1ffielding apr_socket_timeout_set(csd, c->base_server->keep_alive_timeout);
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding update_echo_child_status(c->sbh, SERVER_BUSY_WRITE, NULL, bb);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* Make sure the data is flushed to the client */
2d2eda71267231c2526be701fe655db125852c1ffielding b = apr_bucket_flush_create(c->bucket_alloc);
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet APR_BRIGADE_INSERT_TAIL(bb, b);
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh rv = ap_pass_brigade(c->output_filters, bb);
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet if (rv != APR_SUCCESS) {
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server,
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet "ProtocolEcho: Failure writing to %s",
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet c->remote_ip);
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet break;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb apr_brigade_cleanup(bb);
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* Announce our intent to loop */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb update_echo_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL, NULL);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb apr_brigade_destroy(bb);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ap_time_process_request(c->sbh, STOP_PREQUEST);
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh update_echo_child_status(c->sbh, SERVER_CLOSING, c, NULL);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return OK;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb}
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbstatic const command_rec echo_cmds[] =
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb{
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh AP_INIT_FLAG("ProtocolEcho", echo_on, NULL, RSRC_CONF,
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh "Run an echo server on this host"),
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh { NULL }
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh};
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbstatic void register_hooks(apr_pool_t *p)
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb{
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ap_hook_process_connection(process_echo_connection, NULL, NULL,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb APR_HOOK_MIDDLE);
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh}
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbAP_DECLARE_MODULE(echo) = {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb STANDARD20_MODULE_STUFF,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb NULL, /* create per-directory config structure */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb NULL, /* merge per-directory config structures */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb create_echo_server_config, /* create per-server config structure */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb NULL, /* merge per-server config structures */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh echo_cmds, /* command apr_table_t */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb register_hooks /* register hooks */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb};
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh