af84459fbf938e508fd10b01cb8d699c79083813takashi/* Licensed to the Apache Software Foundation (ASF) under one or more
af84459fbf938e508fd10b01cb8d699c79083813takashi * contributor license agreements. See the NOTICE file distributed with
af84459fbf938e508fd10b01cb8d699c79083813takashi * this work for additional information regarding copyright ownership.
af84459fbf938e508fd10b01cb8d699c79083813takashi * The ASF licenses this file to You under the Apache License, Version 2.0
af84459fbf938e508fd10b01cb8d699c79083813takashi * (the "License"); you may not use this file except in compliance with
af84459fbf938e508fd10b01cb8d699c79083813takashi * the License. You may obtain a copy of the License at
af84459fbf938e508fd10b01cb8d699c79083813takashi *
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen * http://www.apache.org/licenses/LICENSE-2.0
af84459fbf938e508fd10b01cb8d699c79083813takashi *
af84459fbf938e508fd10b01cb8d699c79083813takashi * 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.
af84459fbf938e508fd10b01cb8d699c79083813takashi */
af84459fbf938e508fd10b01cb8d699c79083813takashi
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen#include "ap_config.h"
3f08db06526d6901aa08c110b5bc7dde6bc39905nd#include "ap_mmn.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "httpd.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "http_config.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "http_connection.h"
3f08db06526d6901aa08c110b5bc7dde6bc39905nd#include "http_core.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "http_log.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung#include "apr_buckets.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "apr_strings.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "util_filter.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "scoreboard.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashimodule AP_MODULE_DECLARE_DATA echo_module;
3c13a815670b54d1c17bf02954f7d2b066cde95cnd
3c13a815670b54d1c17bf02954f7d2b066cde95cndtypedef struct {
2d39a41e98476f5235b7c37ce745a4aa0904b1cbrbowen int bEnabled;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe} EchoConfig;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowestatic void *create_echo_server_config(apr_pool_t *p, server_rec *s)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe{
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe EchoConfig *pConfig = apr_pcalloc(p, sizeof *pConfig);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe pConfig->bEnabled = 0;
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe return pConfig;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe}
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashistatic const char *echo_on(cmd_parms *cmd, void *dummy, int arg)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi EchoConfig *pConfig = ap_get_module_config(cmd->server->module_config,
af84459fbf938e508fd10b01cb8d699c79083813takashi &echo_module);
af84459fbf938e508fd10b01cb8d699c79083813takashi pConfig->bEnabled = arg;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi return NULL;
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen}
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashistatic apr_status_t brigade_peek(apr_bucket_brigade *bbIn,
af84459fbf938e508fd10b01cb8d699c79083813takashi char *buff, apr_size_t bufflen)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe{
af84459fbf938e508fd10b01cb8d699c79083813takashi apr_bucket *b;
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh apr_size_t readbytes = 0;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi if (bufflen--)
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen /* compensate for NULL */
af84459fbf938e508fd10b01cb8d699c79083813takashi *buff = '\0';
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe else
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe return APR_EGENERAL;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe if (APR_BRIGADE_EMPTY(bbIn))
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe return APR_EGENERAL;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf b = APR_BRIGADE_FIRST(bbIn);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe while ((b != APR_BRIGADE_SENTINEL(bbIn)) && (readbytes < bufflen)) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe const char *pos;
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen const char *str;
78f97ce162b66a0dbfd7af4dcd9984f162569b04minfrin apr_size_t len;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe apr_status_t rv;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe if ((rv = apr_bucket_read(b, &str, &len, APR_NONBLOCK_READ))
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe != APR_SUCCESS)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe return rv;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe if ((pos = memchr(str, APR_ASCII_LF, len)) != NULL)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe len = pos - str;
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen if (len > bufflen - readbytes)
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen len = bufflen - readbytes;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe memcpy (buff + readbytes, str, len);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe readbytes += len;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe buff[readbytes] = '\0';
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
4bebf996eb7002ebfe897d46a0e0572390604a77nd b = APR_BUCKET_NEXT(b);
4bebf996eb7002ebfe897d46a0e0572390604a77nd }
4bebf996eb7002ebfe897d46a0e0572390604a77nd return APR_SUCCESS;
4bebf996eb7002ebfe897d46a0e0572390604a77nd}
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
4bebf996eb7002ebfe897d46a0e0572390604a77nd
9534272616b71aaea50aeec4162e749a96aebd7fsfstatic int update_echo_child_status(ap_sb_handle_t *sbh,
4bebf996eb7002ebfe897d46a0e0572390604a77nd int status, conn_rec *c,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe apr_bucket_brigade *last_echoed)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi worker_score *ws = ap_get_scoreboard_worker(sbh);
af84459fbf938e508fd10b01cb8d699c79083813takashi int old_status = ws->status;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi ws->status = status;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe if (!ap_extended_status)
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen return old_status;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe ws->last_used = apr_time_now();
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe /* initial pass only, please - in the name of efficiency */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe if (c) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe apr_cpystrn(ws->client,
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen ap_get_remote_host(c, c->base_server->lookup_defaults,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe REMOTE_NOLOOKUP, NULL),
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe sizeof(ws->client));
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe apr_cpystrn(ws->vhost, c->base_server->server_hostname,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe sizeof(ws->vhost));
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe /* Deliberate trailing space - filling in string on WRITE passes */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe apr_cpystrn(ws->request, "ECHO ", sizeof(ws->request));
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe /* each subsequent WRITE pass, let's update what we echoed */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe if (last_echoed) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe brigade_peek(last_echoed, ws->request + sizeof("ECHO ") - 1,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe sizeof(ws->request) - sizeof("ECHO ") + 1);
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe return old_status;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe}
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowestatic int process_echo_connection(conn_rec *c)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe{
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe apr_bucket_brigade *bb;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe apr_bucket *b;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe apr_socket_t *csd = NULL;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe EchoConfig *pConfig = ap_get_module_config(c->base_server->module_config,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe &echo_module);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe if (!pConfig->bEnabled) {
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh return DECLINED;
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh }
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh ap_time_process_request(c->sbh, START_PREQUEST);
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh update_echo_child_status(c->sbh, SERVER_BUSY_READ, c, NULL);
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe bb = apr_brigade_create(c->pool, c->bucket_alloc);
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh for ( ; ; ) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe apr_status_t rv;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh /* Get a single line of input from the client */
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh if (((rv = ap_get_brigade(c->input_filters, bb, AP_MODE_GETLINE,
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh APR_BLOCK_READ, 0)) != APR_SUCCESS)) {
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh apr_brigade_cleanup(bb);
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh if (!APR_STATUS_IS_EOF(rv) && ! APR_STATUS_IS_TIMEUP(rv))
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server, APLOGNO(01611)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe "ProtocolEcho: Failure reading from %s",
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh c->client_ip);
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh break;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe }
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe /* Something horribly wrong happened. Someone didn't block! */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe if (APR_BRIGADE_EMPTY(bb)) {
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server, APLOGNO(01612)
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh "ProtocolEcho: Error - read empty brigade from %s!",
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh c->client_ip);
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh break;
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh }
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh if (!csd) {
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd csd = ap_get_conn_socket(c);
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh apr_socket_timeout_set(csd, c->base_server->keep_alive_timeout);
888cb40bdeec5abf452bd85d6bf63b26d5913d4chumbedooh }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
af84459fbf938e508fd10b01cb8d699c79083813takashi update_echo_child_status(c->sbh, SERVER_BUSY_WRITE, NULL, bb);
af84459fbf938e508fd10b01cb8d699c79083813takashi
2d39a41e98476f5235b7c37ce745a4aa0904b1cbrbowen /* Make sure the data is flushed to the client */
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen b = apr_bucket_flush_create(c->bucket_alloc);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe APR_BRIGADE_INSERT_TAIL(bb, b);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe rv = ap_pass_brigade(c->output_filters, bb);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe if (rv != APR_SUCCESS) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server, APLOGNO(01613)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe "ProtocolEcho: Failure writing to %s",
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe c->client_ip);
c8c717fafa0a09ed13469a603a178921b851dd22igalic break;
c8c717fafa0a09ed13469a603a178921b851dd22igalic }
b7f8d802ecaed65eada1fc31472d06d8460d5528igalic apr_brigade_cleanup(bb);
b7f8d802ecaed65eada1fc31472d06d8460d5528igalic
b7f8d802ecaed65eada1fc31472d06d8460d5528igalic /* Announce our intent to loop */
b7f8d802ecaed65eada1fc31472d06d8460d5528igalic update_echo_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL, NULL);
b7f8d802ecaed65eada1fc31472d06d8460d5528igalic }
b7f8d802ecaed65eada1fc31472d06d8460d5528igalic apr_brigade_destroy(bb);
b7f8d802ecaed65eada1fc31472d06d8460d5528igalic ap_time_process_request(c->sbh, STOP_PREQUEST);
b7f8d802ecaed65eada1fc31472d06d8460d5528igalic update_echo_child_status(c->sbh, SERVER_CLOSING, c, NULL);
b7f8d802ecaed65eada1fc31472d06d8460d5528igalic return OK;
7c7e501f542451bf7225b23cb299ee4228bfe15dgryzor}
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowestatic const command_rec echo_cmds[] =
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe{
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe AP_INIT_FLAG("ProtocolEcho", echo_on, NULL, RSRC_CONF,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe "Run an echo server on this host"),
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe { NULL }
c8c717fafa0a09ed13469a603a178921b851dd22igalic};
7c7e501f542451bf7225b23cb299ee4228bfe15dgryzor
4bebf996eb7002ebfe897d46a0e0572390604a77ndstatic void register_hooks(apr_pool_t *p)
4bebf996eb7002ebfe897d46a0e0572390604a77nd{
4bebf996eb7002ebfe897d46a0e0572390604a77nd ap_hook_process_connection(process_echo_connection, NULL, NULL,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe APR_HOOK_MIDDLE);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe}
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wroweAP_DECLARE_MODULE(echo) = {
8559a67073808d84d85bb5dd552d4247caafe709sf STANDARD20_MODULE_STUFF,
8559a67073808d84d85bb5dd552d4247caafe709sf NULL, /* create per-directory config structure */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe NULL, /* merge per-directory config structures */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe create_echo_server_config, /* create per-server config structure */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe NULL, /* merge per-server config structures */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe echo_cmds, /* command apr_table_t */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe register_hooks /* register hooks */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe};
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe