Apache HTTPD
mod_echo.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ap_config.h"
18#include "ap_mmn.h"
19#include "httpd.h"
20#include "http_config.h"
21#include "http_connection.h"
22#include "http_core.h"
23#include "http_log.h"
24
25#include "apr_buckets.h"
26#include "apr_strings.h"
27#include "util_filter.h"
28#include "scoreboard.h"
29
30module AP_MODULE_DECLARE_DATA echo_module;
31
32typedef struct {
35
37{
39
40 pConfig->bEnabled = 0;
41
42 return pConfig;
43}
44
45static const char *echo_on(cmd_parms *cmd, void *dummy, int arg)
46{
47 EchoConfig *pConfig = ap_get_module_config(cmd->server->module_config,
48 &echo_module);
49 pConfig->bEnabled = arg;
50
51 return NULL;
52}
53
55 char *buff, apr_size_t bufflen)
56{
59
60 if (bufflen--)
61 /* compensate for NULL */
62 *buff = '\0';
63 else
64 return APR_EGENERAL;
65
67 return APR_EGENERAL;
68
70
71 while ((b != APR_BRIGADE_SENTINEL(bbIn)) && (readbytes < bufflen)) {
72 const char *pos;
73 const char *str;
75 apr_status_t rv;
76
78 != APR_SUCCESS)
79 return rv;
80
81 if ((pos = memchr(str, APR_ASCII_LF, len)) != NULL)
82 len = pos - str;
83 if (len > bufflen - readbytes)
86 readbytes += len;
87 buff[readbytes] = '\0';
88
90 }
91 return APR_SUCCESS;
92}
93
94
96 int status, conn_rec *c,
98{
100 int old_status = ws->status;
101
102 ws->status = status;
103
105 return old_status;
106
107 ws->last_used = apr_time_now();
108
109 /* initial pass only, please - in the name of efficiency */
110 if (c) {
111 apr_cpystrn(ws->client64,
112 ap_get_remote_host(c, c->base_server->lookup_defaults,
114 sizeof(ws->client64));
115 apr_cpystrn(ws->vhost, c->base_server->server_hostname,
116 sizeof(ws->vhost));
117 /* Deliberate trailing space - filling in string on WRITE passes */
118 apr_cpystrn(ws->request, "ECHO ", sizeof(ws->request));
119 }
120
121 /* each subsequent WRITE pass, let's update what we echoed */
122 if (last_echoed) {
123 brigade_peek(last_echoed, ws->request + sizeof("ECHO ") - 1,
124 sizeof(ws->request) - sizeof("ECHO ") + 1);
125 }
126
127 return old_status;
128}
129
131{
133 apr_bucket *b;
135 EchoConfig *pConfig = ap_get_module_config(c->base_server->module_config,
136 &echo_module);
137
138 if (!pConfig->bEnabled) {
139 return DECLINED;
140 }
141
144
145 bb = apr_brigade_create(c->pool, c->bucket_alloc);
146
147 for ( ; ; ) {
148 apr_status_t rv;
149
150 /* Get a single line of input from the client */
151 if (((rv = ap_get_brigade(c->input_filters, bb, AP_MODE_GETLINE,
152 APR_BLOCK_READ, 0)) != APR_SUCCESS)) {
154 if (!APR_STATUS_IS_EOF(rv) && ! APR_STATUS_IS_TIMEUP(rv))
155 ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server, APLOGNO(01611)
156 "ProtocolEcho: Failure reading from %s",
157 c->client_ip);
158 break;
159 }
160
161 /* Something horribly wrong happened. Someone didn't block! */
162 if (APR_BRIGADE_EMPTY(bb)) {
163 ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server, APLOGNO(01612)
164 "ProtocolEcho: Error - read empty brigade from %s!",
165 c->client_ip);
166 break;
167 }
168
169 if (!csd) {
171 apr_socket_timeout_set(csd, c->base_server->keep_alive_timeout);
172 }
173
175
176 /* Make sure the data is flushed to the client */
177 b = apr_bucket_flush_create(c->bucket_alloc);
179 rv = ap_pass_brigade(c->output_filters, bb);
180 if (rv != APR_SUCCESS) {
181 ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server, APLOGNO(01613)
182 "ProtocolEcho: Failure writing to %s",
183 c->client_ip);
184 break;
185 }
187
188 /* Announce our intent to loop */
190 }
194 return OK;
195}
196
197static const command_rec echo_cmds[] =
198{
199 AP_INIT_FLAG("ProtocolEcho", echo_on, NULL, RSRC_CONF,
200 "Run an echo server on this host"),
201 { NULL }
202};
203
209
212 NULL, /* create per-directory config structure */
213 NULL, /* merge per-directory config structures */
214 create_echo_server_config, /* create per-server config structure */
215 NULL, /* merge per-server config structures */
216 echo_cmds, /* command apr_table_t */
217 register_hooks /* register hooks */
218};
Symbol export macros and hook functions.
Module Magic Number.
const char * buff
Definition ap_regex.h:186
const char apr_size_t len
Definition ap_regex.h:187
APR-UTIL Buckets/Bucket Brigades.
APR Strings library.
void ap_hook_process_connection(ap_HOOK_process_connection_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition connection.c:42
#define ap_get_module_config(v, m)
#define AP_DECLARE_MODULE(foo)
#define AP_INIT_FLAG(directive, func, mconfig, where, help)
void * csd
#define DECLINED
Definition httpd.h:457
#define OK
Definition httpd.h:456
apr_status_t ap_pass_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket)
apr_status_t ap_get_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
apr_socket_t * ap_get_conn_socket(conn_rec *c)
Definition core.c:5202
#define APLOGNO(n)
Definition http_log.h:117
#define APLOG_INFO
Definition http_log.h:70
#define ap_log_error
Definition http_log.h:370
#define APLOG_MARK
Definition http_log.h:283
void * dummy
Definition http_vhost.h:62
void const char * arg
Definition http_vhost.h:63
#define APR_EGENERAL
Definition apr_errno.h:313
#define APR_STATUS_IS_TIMEUP(s)
Definition apr_errno.h:534
#define APR_STATUS_IS_EOF(s)
Definition apr_errno.h:567
#define APR_BRIGADE_INSERT_TAIL(b, e)
#define APR_BUCKET_NEXT(e)
#define APR_BRIGADE_EMPTY(b)
#define APR_BRIGADE_SENTINEL(b)
#define APR_BRIGADE_FIRST(b)
#define apr_bucket_read(e, str, len, block)
apr_bucket_brigade * bbIn
@ APR_BLOCK_READ
Definition apr_buckets.h:58
@ APR_NONBLOCK_READ
Definition apr_buckets.h:59
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define RSRC_CONF
#define STANDARD20_MODULE_STUFF
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * memchr(const void *s, int c, size_t n)
#define APR_ASCII_LF
Definition apr_general.h:63
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_pool_t * b
Definition apr_pools.h:529
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
const char * s
Definition apr_strings.h:95
apr_cmdtype_e cmd
int int status
const char * ap_get_remote_host(conn_rec *conn, void *dir_config, int type, int *str_is_ip)
Definition core.c:957
#define REMOTE_NOLOOKUP
Definition http_core.h:111
Apache Configuration.
Apache connection library.
CORE HTTP Daemon.
Apache Logging library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
static apr_status_t brigade_peek(apr_bucket_brigade *bbIn, char *buff, apr_size_t bufflen)
Definition mod_echo.c:54
static void * create_echo_server_config(apr_pool_t *p, server_rec *s)
Definition mod_echo.c:36
static const command_rec echo_cmds[]
Definition mod_echo.c:197
static const char * echo_on(cmd_parms *cmd, void *dummy, int arg)
Definition mod_echo.c:45
static void register_hooks(apr_pool_t *p)
Definition mod_echo.c:204
static int update_echo_child_status(ap_sb_handle_t *sbh, int status, conn_rec *c, apr_bucket_brigade *last_echoed)
Definition mod_echo.c:95
static int process_echo_connection(conn_rec *c)
Definition mod_echo.c:130
return NULL
Definition mod_so.c:359
Apache scoreboard library.
#define SERVER_CLOSING
Definition scoreboard.h:64
#define START_PREQUEST
Definition scoreboard.h:249
#define SERVER_BUSY_WRITE
Definition scoreboard.h:60
#define SERVER_BUSY_READ
Definition scoreboard.h:59
void ap_time_process_request(ap_sb_handle_t *sbh, int status)
Definition scoreboard.c:632
int ap_extended_status
Definition scoreboard.c:61
#define STOP_PREQUEST
Definition scoreboard.h:250
#define SERVER_BUSY_KEEPALIVE
Definition scoreboard.h:61
worker_score * ap_get_scoreboard_worker(ap_sb_handle_t *sbh)
Definition scoreboard.c:676
int bEnabled
Definition mod_echo.c:33
Structure to store things which are per connection.
Definition httpd.h:1152
A structure to store information for each virtual server.
Definition httpd.h:1322
apr_status_t apr_socket_timeout_set(apr_socket_t *sock, apr_interval_time_t t)
Definition sockopt.c:75
#define str
Apache filter library.
@ AP_MODE_GETLINE
Definition util_filter.h:48