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