【问题标题】:Teach Apache2 a new Protocoll教 Apache2 一个新的协议
【发布时间】:2022-11-11 01:09:38
【问题描述】:

我想为 Apache2 编写一个模块。 我想实现 rtmp 协议。

所以我从一个简单的模块开始,注册了一个 Handler、HTTP Sheme 和 Port。

typedef uint bool;

typedef struct {
    bool enabled;
} rtmp_config;

static rtmp_config config;

const char *rtmp_set_enabled(cmd_parms *cmd, void *cfg, const char *arg)
{
    if(!strcasecmp(arg, "on")) config.enabled = 1;
    else config.enabled = 0;
    return NULL;
}

static const command_rec rtmp_directives[] = {
    AP_INIT_TAKE1("rtmp_enabled", rtmp_set_enabled, NULL, RSRC_CONF, "Enable or disable mod_rtmp"),
    { NULL }
};

static const char* rtmp_sheme_hook(const request_rec *r){
    if(config.enabled == FALSE) return NULL;
    return "rtmp";
}

static apr_port_t rtmp_default_port(const request_rec *r){
    if(config.enabled == FALSE) return 0;
    return 1935;
}

/* The sample content handler */
static int mod_rtmp_handler(request_rec *r)
{
    if (strcmp(r->handler, "mod_rtmp")) {
        return DECLINED;
    }

    r->content_type = "text/html";      

    ap_rputs("3", r);

    return OK;
}

static void mod_rtmp_register_hooks(apr_pool_t *p)
{
    config.enabled = TRUE;
    ap_hook_handler(mod_rtmp_handler, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_http_scheme(rtmp_sheme_hook, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_default_port(rtmp_default_port, NULL, NULL, APR_HOOK_MIDDLE);
}

/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA mod_rtmp_module = {
    STANDARD20_MODULE_STUFF, 
    NULL,                  /* create per-dir    config structures */
    NULL,                  /* merge  per-dir    config structures */
    NULL,                  /* create per-server config structures */
    NULL,                  /* merge  per-server config structures */
    rtmp_directives,       /* table of config file commands       */
    mod_rtmp_register_hooks  /* register hooks                      */
};

它编译得很好。

我在虚拟主机中激活了它:

<VirtualHost *:1935>
        LogLevel debug

        Protocols rtmp RTMP
        rtmp_enabled on
        <Location "/">
                SetHandler mod_rtmp
                Require all granted
        </Location>

</VirtualHost>

我用 curl 开始请求:rtmp://localhost/

Apache2 服务器使用以下行拒绝此请求:

[Sun Dec 05 23:34:23.251495 2021] [core:debug] [pid 603:tid 140402319763200] protocol.c(1447): [client ::1:37804] AH00566: request failed: malformed request line

访问日志如下所示

::1 - - [05/Dec/2021:23:34:23 +0100] "\x03" 400 485 "-" "-"

为什么此请求被拒绝? 我能对 apache 做些什么让我来处理这个?

此致

【问题讨论】:

    标签: apache request protocols rtmp


    【解决方案1】:

    我在问题中提供的代码非常接近。

    rtmp_register_hooks 函数错过了一个需要的钩子:

    ap_hook_process_connection(rtmp_hook_process_connection, NULL, NULL, APR_HOOK_MIDDLE);
    

    这个钩子让我处理所有传入的流量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 2020-10-28
      • 2023-03-18
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多