c - 简单的 Apache 服务器模块

标签 c apache apache2

我根据在某处找到的示例 hello world 模块创建了一个简单的 Apache 服务器模块。然后我添加了一个变量来跟踪我页面上的访问次数。这是我的模块代码:

/* The simplest HelloWorld module */
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>

static int noOfViews = 0;

static int helloworld_handler(request_rec *r)
{
    noOfViews++;

    if (!r->handler || strcmp(r->handler, "helloworld")) {
        return DECLINED;
    }

    if (r->method_number != M_GET) {
        return HTTP_METHOD_NOT_ALLOWED;
    }

    ap_set_content_type(r, "text/html;charset=ascii");
    ap_rputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n",
             r);
    ap_rputs("<html><head><title>Apache HelloWorld "
             "Module</title></head>", r);
    ap_rputs("<body><h1>Hello World!</h1>", r);
    ap_rputs("<p>This is the Apache HelloWorld module!</p>", r);
    ap_rprintf(r, "<p>Views: %d</p>", noOfViews);
    ap_rputs("</body></html>", r);
    return OK;
}

static void helloworld_hooks(apr_pool_t *pool)
{
    ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

module AP_MODULE_DECLARE_DATA helloworld_module = {
    STANDARD20_MODULE_STUFF,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
            helloworld_hooks
};

我的模块目前遇到了 2 个我无法解决的问题。

  1. 我的观看次数似乎增加了 2 的倍数,尽管我只希望一次增加 1。

  2. 当我不断刷新页面时,有时我的数字会随机下降。

有谁知道我的问题的根源是什么?

非常感谢你们!

最佳答案

  1. 您正在为您实际未处理的请求递增计数器。

  2. Apache 中的每个工作进程都有自己的 noOfViews 副本。无论您使用的是 prefork 还是 worker MPM,这都适用;这只是 prefork 的一个更明显的问题。

关于c - 简单的 Apache 服务器模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8348571/

相关文章:

Apache 网址映射

apache - 有名为 'apache2' 的进程正在运行

ssl - apache 2.4.25 密码强度问题

c - 为什么我得到对 pthread_mutexattr_settype 的 undefined reference ?

php - Apache "localhost is currently unable to handle this request."php 请求

apache2 - 我安装了 apache 2,但在 sudo ufw 应用程序列表中,应用程序列表中没有 apache 应用程序

docker - 如何在 apache2 上使用 pthread?

c - 在 c 中使用二维结构数组时出现段错误

c - C 中的 realloc 字符串表在第四次迭代时崩溃

C Unsigned int 提供负值?