mod_echo.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "ap_config.h"
  17. #include "ap_mmn.h"
  18. #include "httpd.h"
  19. #include "http_config.h"
  20. #include "http_connection.h"
  21. #include "apr_buckets.h"
  22. #include "util_filter.h"
  23. module AP_MODULE_DECLARE_DATA echo_module;
  24. typedef struct {
  25. int bEnabled;
  26. } EchoConfig;
  27. static void *create_echo_server_config(apr_pool_t *p, server_rec *s)
  28. {
  29. EchoConfig *pConfig = apr_pcalloc(p, sizeof *pConfig);
  30. pConfig->bEnabled = 0;
  31. return pConfig;
  32. }
  33. static const char *echo_on(cmd_parms *cmd, void *dummy, int arg)
  34. {
  35. EchoConfig *pConfig = ap_get_module_config(cmd->server->module_config,
  36. &echo_module);
  37. pConfig->bEnabled = arg;
  38. return NULL;
  39. }
  40. static int process_echo_connection(conn_rec *c)
  41. {
  42. apr_bucket_brigade *bb;
  43. apr_bucket *b;
  44. apr_status_t rv;
  45. EchoConfig *pConfig = ap_get_module_config(c->base_server->module_config,
  46. &echo_module);
  47. if (!pConfig->bEnabled) {
  48. return DECLINED;
  49. }
  50. do {
  51. bb = apr_brigade_create(c->pool, c->bucket_alloc);
  52. /* Get a single line of input from the client */
  53. if (((rv = ap_get_brigade(c->input_filters, bb, AP_MODE_GETLINE,
  54. APR_BLOCK_READ, 0)) != APR_SUCCESS) ||
  55. APR_BRIGADE_EMPTY(bb)) {
  56. apr_brigade_destroy(bb);
  57. break;
  58. }
  59. /* Make sure the data is flushed to the client */
  60. b = apr_bucket_flush_create(c->bucket_alloc);
  61. APR_BRIGADE_INSERT_TAIL(bb, b);
  62. /* Send back the data. */
  63. rv = ap_pass_brigade(c->output_filters, bb);
  64. } while (rv == APR_SUCCESS);
  65. return OK;
  66. }
  67. static const command_rec echo_cmds[] =
  68. {
  69. AP_INIT_FLAG("ProtocolEcho", echo_on, NULL, RSRC_CONF,
  70. "Run an echo server on this host"),
  71. { NULL }
  72. };
  73. static void register_hooks(apr_pool_t *p)
  74. {
  75. ap_hook_process_connection(process_echo_connection, NULL, NULL,
  76. APR_HOOK_MIDDLE);
  77. }
  78. module AP_MODULE_DECLARE_DATA echo_module = {
  79. STANDARD20_MODULE_STUFF,
  80. NULL, /* create per-directory config structure */
  81. NULL, /* merge per-directory config structures */
  82. create_echo_server_config, /* create per-server config structure */
  83. NULL, /* merge per-server config structures */
  84. echo_cmds, /* command apr_table_t */
  85. register_hooks /* register hooks */
  86. };