1 Star 0 Fork 132

M.R.Z/nginx-http-flv-module

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ngx_rtmp_variables.c 32.20 KB
一键复制 编辑 原始数据 按行查看 历史
winshining 提交于 2018-12-25 11:16 . [dev] remove unused code lines.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
* Copyright (C) Winshining
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <nginx.h>
#include "ngx_rtmp.h"
static ngx_rtmp_variable_t *ngx_rtmp_add_prefix_variable(ngx_conf_t *cf,
ngx_str_t *name, ngx_uint_t flags);
static ngx_int_t ngx_rtmp_variable_request(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_argument(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
#if (NGX_HAVE_TCP_INFO)
static ngx_int_t ngx_rtmp_variable_tcpinfo(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
#endif
static ngx_int_t ngx_rtmp_variable_host(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_binary_remote_addr(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_remote_addr(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_remote_port(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_server_addr(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_server_port(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static void ngx_rtmp_variable_set_args(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_is_args(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_server_name(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_bytes_sent(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_request_time(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_request_id(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_connection(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_nginx_version(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_hostname(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_pid(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_rtmp_variable_msec(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data);
static ngx_rtmp_variable_t ngx_rtmp_core_variables[] = {
{ ngx_string("host"), NULL, ngx_rtmp_variable_host, 0, 0, 0 },
{ ngx_string("binary_remote_addr"), NULL,
ngx_rtmp_variable_binary_remote_addr, 0, 0, 0 },
{ ngx_string("remote_addr"), NULL, ngx_rtmp_variable_remote_addr, 0, 0, 0 },
{ ngx_string("remote_port"), NULL, ngx_rtmp_variable_remote_port, 0, 0, 0 },
{ ngx_string("server_addr"), NULL, ngx_rtmp_variable_server_addr, 0, 0, 0 },
{ ngx_string("server_port"), NULL, ngx_rtmp_variable_server_port, 0, 0, 0 },
{ ngx_string("request_uri"), NULL, ngx_rtmp_variable_request,
offsetof(ngx_rtmp_session_t, unparsed_uri), 0, 0 },
{ ngx_string("uri"), NULL, ngx_rtmp_variable_request,
offsetof(ngx_rtmp_session_t, uri),
NGX_RTMP_VAR_NOCACHEABLE, 0 },
{ ngx_string("query_string"), NULL, ngx_rtmp_variable_request,
offsetof(ngx_rtmp_session_t, args),
NGX_RTMP_VAR_NOCACHEABLE, 0 },
{ ngx_string("args"),
ngx_rtmp_variable_set_args,
ngx_rtmp_variable_request,
offsetof(ngx_rtmp_session_t, args),
NGX_RTMP_VAR_CHANGEABLE|NGX_RTMP_VAR_NOCACHEABLE, 0 },
{ ngx_string("is_args"), NULL, ngx_rtmp_variable_is_args,
0, NGX_RTMP_VAR_NOCACHEABLE, 0 },
{ ngx_string("server_name"), NULL, ngx_rtmp_variable_server_name, 0, 0, 0 },
{ ngx_string("bytes_sent"), NULL, ngx_rtmp_variable_bytes_sent,
0, 0, 0 },
{ ngx_string("request_time"), NULL, ngx_rtmp_variable_request_time,
0, NGX_RTMP_VAR_NOCACHEABLE, 0 },
{ ngx_string("request_id"), NULL,
ngx_rtmp_variable_request_id,
0, 0, 0 },
{ ngx_string("connection"), NULL,
ngx_rtmp_variable_connection, 0, 0, 0 },
{ ngx_string("nginx_version"), NULL, ngx_rtmp_variable_nginx_version,
0, 0, 0 },
{ ngx_string("hostname"), NULL, ngx_rtmp_variable_hostname,
0, 0, 0 },
{ ngx_string("pid"), NULL, ngx_rtmp_variable_pid,
0, 0, 0 },
{ ngx_string("msec"), NULL, ngx_rtmp_variable_msec,
0, NGX_RTMP_VAR_NOCACHEABLE, 0 },
#if (NGX_HAVE_TCP_INFO)
{ ngx_string("tcpinfo_rtt"), NULL, ngx_rtmp_variable_tcpinfo,
0, NGX_RTMP_VAR_NOCACHEABLE, 0 },
{ ngx_string("tcpinfo_rttvar"), NULL, ngx_rtmp_variable_tcpinfo,
1, NGX_RTMP_VAR_NOCACHEABLE, 0 },
{ ngx_string("tcpinfo_snd_cwnd"), NULL, ngx_rtmp_variable_tcpinfo,
2, NGX_RTMP_VAR_NOCACHEABLE, 0 },
{ ngx_string("tcpinfo_rcv_space"), NULL, ngx_rtmp_variable_tcpinfo,
3, NGX_RTMP_VAR_NOCACHEABLE, 0 },
#endif
{ ngx_string("arg_"), NULL, ngx_rtmp_variable_argument,
0, NGX_RTMP_VAR_NOCACHEABLE|NGX_RTMP_VAR_PREFIX, 0 },
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
ngx_rtmp_variable_value_t ngx_rtmp_variable_null_value =
ngx_rtmp_variable("");
ngx_rtmp_variable_value_t ngx_rtmp_variable_true_value =
ngx_rtmp_variable("1");
static ngx_uint_t ngx_rtmp_variable_depth = 100;
ngx_rtmp_variable_t *
ngx_rtmp_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)
{
ngx_int_t rc;
ngx_uint_t i;
ngx_hash_key_t *key;
ngx_rtmp_variable_t *v;
ngx_rtmp_core_main_conf_t *cmcf;
if (name->len == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"$\"");
return NULL;
}
if (flags & NGX_RTMP_VAR_PREFIX) {
return ngx_rtmp_add_prefix_variable(cf, name, flags);
}
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
key = cmcf->variables_keys->keys.elts;
for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) {
if (name->len != key[i].key.len
|| ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0)
{
continue;
}
v = key[i].value;
if (!(v->flags & NGX_RTMP_VAR_CHANGEABLE)) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"the duplicate \"%V\" variable", name);
return NULL;
}
v->flags &= flags | ~NGX_RTMP_VAR_WEAK;
return v;
}
v = ngx_palloc(cf->pool, sizeof(ngx_rtmp_variable_t));
if (v == NULL) {
return NULL;
}
v->name.len = name->len;
v->name.data = ngx_pnalloc(cf->pool, name->len);
if (v->name.data == NULL) {
return NULL;
}
ngx_strlow(v->name.data, name->data, name->len);
v->set_handler = NULL;
v->get_handler = NULL;
v->data = 0;
v->flags = flags;
v->index = 0;
rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0);
if (rc == NGX_ERROR) {
return NULL;
}
if (rc == NGX_BUSY) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"conflicting variable name \"%V\"", name);
return NULL;
}
return v;
}
static ngx_rtmp_variable_t *
ngx_rtmp_add_prefix_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)
{
ngx_uint_t i;
ngx_rtmp_variable_t *v;
ngx_rtmp_core_main_conf_t *cmcf;
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
v = cmcf->prefix_variables.elts;
for (i = 0; i < cmcf->prefix_variables.nelts; i++) {
if (name->len != v[i].name.len
|| ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0)
{
continue;
}
v = &v[i];
if (!(v->flags & NGX_RTMP_VAR_CHANGEABLE)) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"the duplicate \"%V\" variable", name);
return NULL;
}
v->flags &= flags | ~NGX_RTMP_VAR_WEAK;
return v;
}
v = ngx_array_push(&cmcf->prefix_variables);
if (v == NULL) {
return NULL;
}
v->name.len = name->len;
v->name.data = ngx_pnalloc(cf->pool, name->len);
if (v->name.data == NULL) {
return NULL;
}
ngx_strlow(v->name.data, name->data, name->len);
v->set_handler = NULL;
v->get_handler = NULL;
v->data = 0;
v->flags = flags;
v->index = 0;
return v;
}
ngx_int_t
ngx_rtmp_get_variable_index(ngx_conf_t *cf, ngx_str_t *name)
{
ngx_uint_t i;
ngx_rtmp_variable_t *v;
ngx_rtmp_core_main_conf_t *cmcf;
if (name->len == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"$\"");
return NGX_ERROR;
}
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
v = cmcf->variables.elts;
if (v == NULL) {
if (ngx_array_init(&cmcf->variables, cf->pool, 4,
sizeof(ngx_rtmp_variable_t))
!= NGX_OK)
{
return NGX_ERROR;
}
} else {
for (i = 0; i < cmcf->variables.nelts; i++) {
if (name->len != v[i].name.len
|| ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0)
{
continue;
}
return i;
}
}
v = ngx_array_push(&cmcf->variables);
if (v == NULL) {
return NGX_ERROR;
}
v->name.len = name->len;
v->name.data = ngx_pnalloc(cf->pool, name->len);
if (v->name.data == NULL) {
return NGX_ERROR;
}
ngx_strlow(v->name.data, name->data, name->len);
v->set_handler = NULL;
v->get_handler = NULL;
v->data = 0;
v->flags = 0;
v->index = cmcf->variables.nelts - 1;
return v->index;
}
ngx_rtmp_variable_value_t *
ngx_rtmp_get_indexed_variable(ngx_rtmp_session_t *s, ngx_uint_t index)
{
ngx_rtmp_variable_t *v;
ngx_rtmp_core_main_conf_t *cmcf;
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
if (cmcf->variables.nelts <= index) {
ngx_log_error(NGX_LOG_ALERT, s->connection->log, 0,
"unknown variable index: %ui", index);
return NULL;
}
if (s->variables[index].not_found || s->variables[index].valid) {
return &s->variables[index];
}
v = cmcf->variables.elts;
if (ngx_rtmp_variable_depth == 0) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"cycle while evaluating variable \"%V\"",
&v[index].name);
return NULL;
}
ngx_rtmp_variable_depth--;
if (v[index].get_handler(s, &s->variables[index], v[index].data)
== NGX_OK)
{
ngx_rtmp_variable_depth++;
if (v[index].flags & NGX_RTMP_VAR_NOCACHEABLE) {
s->variables[index].no_cacheable = 1;
}
return &s->variables[index];
}
ngx_rtmp_variable_depth++;
s->variables[index].valid = 0;
s->variables[index].not_found = 1;
return NULL;
}
ngx_rtmp_variable_value_t *
ngx_rtmp_get_flushed_variable(ngx_rtmp_session_t *s, ngx_uint_t index)
{
ngx_rtmp_variable_value_t *v;
v = &s->variables[index];
if (v->valid || v->not_found) {
if (!v->no_cacheable) {
return v;
}
v->valid = 0;
v->not_found = 0;
}
return ngx_rtmp_get_indexed_variable(s, index);
}
ngx_rtmp_variable_value_t *
ngx_rtmp_get_variable(ngx_rtmp_session_t *s, ngx_str_t *name, ngx_uint_t key)
{
size_t len;
ngx_uint_t i, n;
ngx_rtmp_variable_t *v;
ngx_rtmp_variable_value_t *vv;
ngx_rtmp_core_main_conf_t *cmcf;
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
v = ngx_hash_find(&cmcf->variables_hash, key, name->data, name->len);
if (v) {
if (v->flags & NGX_RTMP_VAR_INDEXED) {
return ngx_rtmp_get_flushed_variable(s, v->index);
}
if (ngx_rtmp_variable_depth == 0) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"cycle while evaluating variable \"%V\"", name);
return NULL;
}
ngx_rtmp_variable_depth--;
vv = ngx_palloc(s->connection->pool, sizeof(ngx_rtmp_variable_value_t));
if (vv && v->get_handler(s, vv, v->data) == NGX_OK) {
ngx_rtmp_variable_depth++;
return vv;
}
ngx_rtmp_variable_depth++;
return NULL;
}
vv = ngx_palloc(s->connection->pool, sizeof(ngx_rtmp_variable_value_t));
if (vv == NULL) {
return NULL;
}
len = 0;
v = cmcf->prefix_variables.elts;
n = cmcf->prefix_variables.nelts;
for (i = 0; i < cmcf->prefix_variables.nelts; i++) {
if (name->len >= v[i].name.len && name->len > len
&& ngx_strncmp(name->data, v[i].name.data, v[i].name.len) == 0)
{
len = v[i].name.len;
n = i;
}
}
if (n != cmcf->prefix_variables.nelts) {
if (v[n].get_handler(s, vv, (uintptr_t) name) == NGX_OK) {
return vv;
}
return NULL;
}
vv->not_found = 1;
return vv;
}
static ngx_int_t
ngx_rtmp_variable_request(ngx_rtmp_session_t *s, ngx_rtmp_variable_value_t *v,
uintptr_t data)
{
ngx_str_t *str;
str = (ngx_str_t *) ((char *) s + data);
if (str->data) {
v->len = str->len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = str->data;
} else {
v->not_found = 1;
}
return NGX_OK;
}
ngx_int_t
ngx_rtmp_arg(ngx_rtmp_session_t *s, u_char *name, size_t len, ngx_str_t *value)
{
u_char *p, *last;
if (s->args.len == 0) {
return NGX_DECLINED;
}
p = s->args.data;
last = p + s->args.len;
for ( /* void */ ; p < last; p++) {
/* we need '=' after name, so drop one char from last */
p = ngx_strlcasestrn(p, last - 1, name, len - 1);
if (p == NULL) {
return NGX_DECLINED;
}
if ((p == s->args.data || *(p - 1) == '&') && *(p + len) == '=') {
value->data = p + len + 1;
p = ngx_strlchr(p, last, '&');
if (p == NULL) {
p = s->args.data + s->args.len;
}
value->len = p - value->data;
return NGX_OK;
}
}
return NGX_DECLINED;
}
static ngx_int_t
ngx_rtmp_variable_argument(ngx_rtmp_session_t *s, ngx_rtmp_variable_value_t *v,
uintptr_t data)
{
ngx_str_t *name = (ngx_str_t *) data;
u_char *arg;
size_t len;
ngx_str_t value;
len = name->len - (sizeof("arg_") - 1);
arg = name->data + sizeof("arg_") - 1;
if (ngx_rtmp_arg(s, arg, len, &value) != NGX_OK) {
v->not_found = 1;
return NGX_OK;
}
v->data = value.data;
v->len = value.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
#if (NGX_HAVE_TCP_INFO)
static ngx_int_t
ngx_rtmp_variable_tcpinfo(ngx_rtmp_session_t *s, ngx_rtmp_variable_value_t *v,
uintptr_t data)
{
struct tcp_info ti;
socklen_t len;
uint32_t value;
len = sizeof(struct tcp_info);
if (getsockopt(s->connection->fd, IPPROTO_TCP, TCP_INFO, &ti, &len) == -1) {
v->not_found = 1;
return NGX_OK;
}
v->data = ngx_pnalloc(s->connection->pool, NGX_INT32_LEN);
if (v->data == NULL) {
return NGX_ERROR;
}
switch (data) {
case 0:
value = ti.tcpi_rtt;
break;
case 1:
value = ti.tcpi_rttvar;
break;
case 2:
value = ti.tcpi_snd_cwnd;
break;
case 3:
value = ti.tcpi_rcv_space;
break;
/* suppress warning */
default:
value = 0;
break;
}
v->len = ngx_sprintf(v->data, "%uD", value) - v->data;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
#endif
static ngx_int_t
ngx_rtmp_variable_host(ngx_rtmp_session_t *s, ngx_rtmp_variable_value_t *v,
uintptr_t data)
{
v->len = s->host_end - s->host_start;
v->data = s->host_start;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_binary_remote_addr(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
switch (s->connection->sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) s->connection->sockaddr;
v->len = sizeof(struct in6_addr);
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = sin6->sin6_addr.s6_addr;
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) s->connection->sockaddr;
v->len = sizeof(in_addr_t);
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = (u_char *) &sin->sin_addr;
break;
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_remote_addr(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
v->len = s->connection->addr_text.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = s->connection->addr_text.data;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_remote_port(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
ngx_uint_t port;
v->len = 0;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = ngx_pnalloc(s->connection->pool, sizeof("65535") - 1);
if (v->data == NULL) {
return NGX_ERROR;
}
port = ngx_inet_get_port(s->connection->sockaddr);
if (port > 0 && port < 65536) {
v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_server_addr(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
ngx_str_t str;
u_char addr[NGX_SOCKADDR_STRLEN];
str.len = NGX_SOCKADDR_STRLEN;
str.data = addr;
if (ngx_connection_local_sockaddr(s->connection, &str, 0) != NGX_OK) {
return NGX_ERROR;
}
str.data = ngx_pnalloc(s->connection->pool, str.len);
if (str.data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(str.data, addr, str.len);
v->len = str.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = str.data;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_server_port(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
ngx_uint_t port;
v->len = 0;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
if (ngx_connection_local_sockaddr(s->connection, NULL, 0) != NGX_OK) {
return NGX_ERROR;
}
v->data = ngx_pnalloc(s->connection->pool, sizeof("65535") - 1);
if (v->data == NULL) {
return NGX_ERROR;
}
port = ngx_inet_get_port(s->connection->local_sockaddr);
if (port > 0 && port < 65536) {
v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
}
return NGX_OK;
}
static void
ngx_rtmp_variable_set_args(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
s->args.len = v->len;
s->args.data = v->data;
s->valid_unparsed_uri = 0;
}
static ngx_int_t
ngx_rtmp_variable_is_args(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
if (s->args.len == 0) {
v->len = 0;
v->data = NULL;
return NGX_OK;
}
v->len = 1;
v->data = (u_char *) "?";
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_server_name(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
ngx_rtmp_core_srv_conf_t *cscf;
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
v->len = cscf->server_name.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = cscf->server_name.data;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_bytes_sent(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
u_char *p;
p = ngx_pnalloc(s->connection->pool, NGX_OFF_T_LEN);
if (p == NULL) {
return NGX_ERROR;
}
v->len = ngx_sprintf(p, "%O", s->connection->sent) - p;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = p;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_request_time(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
u_char *p;
ngx_time_t *tp;
ngx_msec_int_t ms;
p = ngx_pnalloc(s->connection->pool, NGX_TIME_T_LEN + 4);
if (p == NULL) {
return NGX_ERROR;
}
tp = ngx_timeofday();
ms = (ngx_msec_int_t)
((tp->sec - s->start_sec) * 1000 + (tp->msec - s->start_msec));
ms = ngx_max(ms, 0);
v->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = p;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_request_id(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
u_char *id;
#if (NGX_OPENSSL)
u_char random_bytes[16];
#endif
id = ngx_pnalloc(s->connection->pool, 32);
if (id == NULL) {
return NGX_ERROR;
}
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->len = 32;
v->data = id;
#if (NGX_OPENSSL)
if (RAND_bytes(random_bytes, 16) == 1) {
ngx_hex_dump(id, random_bytes, 16);
return NGX_OK;
}
ngx_ssl_error(NGX_LOG_ERR, s->connection->log, 0, "RAND_bytes() failed");
#endif
ngx_sprintf(id, "%08xD%08xD%08xD%08xD",
(uint32_t) ngx_random(), (uint32_t) ngx_random(),
(uint32_t) ngx_random(), (uint32_t) ngx_random());
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_connection(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
u_char *p;
p = ngx_pnalloc(s->connection->pool, NGX_ATOMIC_T_LEN);
if (p == NULL) {
return NGX_ERROR;
}
v->len = ngx_sprintf(p, "%uA", s->connection->number) - p;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = p;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_nginx_version(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
v->len = sizeof(NGINX_VERSION) - 1;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = (u_char *) NGINX_VERSION;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_hostname(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
v->len = ngx_cycle->hostname.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = ngx_cycle->hostname.data;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_pid(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
u_char *p;
p = ngx_pnalloc(s->connection->pool, NGX_INT64_LEN);
if (p == NULL) {
return NGX_ERROR;
}
v->len = ngx_sprintf(p, "%P", ngx_pid) - p;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = p;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_variable_msec(ngx_rtmp_session_t *s,
ngx_rtmp_variable_value_t *v, uintptr_t data)
{
u_char *p;
ngx_time_t *tp;
p = ngx_pnalloc(s->connection->pool, NGX_TIME_T_LEN + 4);
if (p == NULL) {
return NGX_ERROR;
}
tp = ngx_timeofday();
v->len = ngx_sprintf(p, "%T.%03M", tp->sec, tp->msec) - p;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = p;
return NGX_OK;
}
void *
ngx_rtmp_map_find(ngx_rtmp_session_t *s, ngx_rtmp_map_t *map, ngx_str_t *match)
{
void *value;
u_char *low;
size_t len;
ngx_uint_t key;
len = match->len;
if (len) {
low = ngx_pnalloc(s->connection->pool, len);
if (low == NULL) {
return NULL;
}
} else {
low = NULL;
}
key = ngx_hash_strlow(low, match->data, len);
value = ngx_hash_find_combined(&map->hash, key, low, len);
if (value) {
return value;
}
#if (NGX_PCRE)
if (len && map->nregex) {
ngx_int_t n;
ngx_uint_t i;
ngx_rtmp_map_regex_t *reg;
reg = map->regex;
for (i = 0; i < map->nregex; i++) {
n = ngx_rtmp_regex_exec(s, reg[i].regex, match);
if (n == NGX_OK) {
return reg[i].value;
}
if (n == NGX_DECLINED) {
continue;
}
/* NGX_ERROR */
return NULL;
}
}
#endif
return NULL;
}
#if (NGX_PCRE)
static ngx_int_t
ngx_rtmp_variable_not_found(ngx_rtmp_session_t *s, ngx_rtmp_variable_value_t *v,
uintptr_t data)
{
v->not_found = 1;
return NGX_OK;
}
ngx_rtmp_regex_t *
ngx_rtmp_regex_compile(ngx_conf_t *cf, ngx_regex_compile_t *rc)
{
u_char *p;
size_t size;
ngx_str_t name;
ngx_uint_t i, n;
ngx_rtmp_variable_t *v;
ngx_rtmp_regex_t *re;
ngx_rtmp_regex_variable_t *rv;
ngx_rtmp_core_main_conf_t *cmcf;
rc->pool = cf->pool;
if (ngx_regex_compile(rc) != NGX_OK) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc->err);
return NULL;
}
re = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_regex_t));
if (re == NULL) {
return NULL;
}
re->regex = rc->regex;
re->ncaptures = rc->captures;
re->name = rc->pattern;
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
cmcf->ncaptures = ngx_max(cmcf->ncaptures, re->ncaptures);
n = (ngx_uint_t) rc->named_captures;
if (n == 0) {
return re;
}
rv = ngx_palloc(rc->pool, n * sizeof(ngx_rtmp_regex_variable_t));
if (rv == NULL) {
return NULL;
}
re->variables = rv;
re->nvariables = n;
size = rc->name_size;
p = rc->names;
for (i = 0; i < n; i++) {
rv[i].capture = 2 * ((p[0] << 8) + p[1]);
name.data = &p[2];
name.len = ngx_strlen(name.data);
v = ngx_rtmp_add_variable(cf, &name, NGX_RTMP_VAR_CHANGEABLE);
if (v == NULL) {
return NULL;
}
rv[i].index = ngx_rtmp_get_variable_index(cf, &name);
if (rv[i].index == NGX_ERROR) {
return NULL;
}
v->get_handler = ngx_rtmp_variable_not_found;
p += size;
}
return re;
}
ngx_int_t
ngx_rtmp_regex_exec(ngx_rtmp_session_t *s, ngx_rtmp_regex_t *re, ngx_str_t *str)
{
ngx_int_t rc, index;
ngx_uint_t i, n, len;
ngx_rtmp_variable_value_t *vv;
ngx_rtmp_core_main_conf_t *cmcf;
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
if (re->ncaptures) {
len = cmcf->ncaptures;
if (s->captures == NULL) {
s->captures = ngx_palloc(s->connection->pool, len * sizeof(int));
if (s->captures == NULL) {
return NGX_ERROR;
}
}
} else {
len = 0;
}
rc = ngx_regex_exec(re->regex, str, s->captures, len);
if (rc == NGX_REGEX_NO_MATCHED) {
return NGX_DECLINED;
}
if (rc < 0) {
ngx_log_error(NGX_LOG_ALERT, s->connection->log, 0,
ngx_regex_exec_n " failed: %i on \"%V\" using \"%V\"",
rc, str, &re->name);
return NGX_ERROR;
}
for (i = 0; i < re->nvariables; i++) {
n = re->variables[i].capture;
index = re->variables[i].index;
vv = &s->variables[index];
vv->len = s->captures[n + 1] - s->captures[n];
vv->valid = 1;
vv->no_cacheable = 0;
vv->not_found = 0;
vv->data = &str->data[s->captures[n]];
#if (NGX_DEBUG)
{
ngx_rtmp_variable_t *v;
v = cmcf->variables.elts;
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"http regex set $%V to \"%v\"", &v[index].name, vv);
}
#endif
}
s->ncaptures = rc * 2;
s->captures_data = str->data;
return NGX_OK;
}
#endif
ngx_int_t
ngx_rtmp_variables_add_core_vars(ngx_conf_t *cf)
{
ngx_rtmp_variable_t *cv, *v;
ngx_rtmp_core_main_conf_t *cmcf;
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
cmcf->variables_keys = ngx_pcalloc(cf->temp_pool,
sizeof(ngx_hash_keys_arrays_t));
if (cmcf->variables_keys == NULL) {
return NGX_ERROR;
}
cmcf->variables_keys->pool = cf->pool;
cmcf->variables_keys->temp_pool = cf->pool;
if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL)
!= NGX_OK)
{
return NGX_ERROR;
}
if (ngx_array_init(&cmcf->prefix_variables, cf->pool, 8,
sizeof(ngx_rtmp_variable_t))
!= NGX_OK)
{
return NGX_ERROR;
}
for (cv = ngx_rtmp_core_variables; cv->name.len; cv++) {
v = ngx_rtmp_add_variable(cf, &cv->name, cv->flags);
if (v == NULL) {
return NGX_ERROR;
}
*v = *cv;
}
return NGX_OK;
}
ngx_int_t
ngx_rtmp_variables_init_vars(ngx_conf_t *cf)
{
size_t len;
ngx_uint_t i, n;
ngx_hash_key_t *key;
ngx_hash_init_t hash;
ngx_rtmp_variable_t *v, *av, *pv;
ngx_rtmp_core_main_conf_t *cmcf;
/* set the handlers for the indexed http variables */
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
v = cmcf->variables.elts;
pv = cmcf->prefix_variables.elts;
key = cmcf->variables_keys->keys.elts;
for (i = 0; i < cmcf->variables.nelts; i++) {
for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {
av = key[n].value;
if (v[i].name.len == key[n].key.len
&& ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len)
== 0)
{
v[i].get_handler = av->get_handler;
v[i].data = av->data;
av->flags |= NGX_RTMP_VAR_INDEXED;
v[i].flags = av->flags;
av->index = i;
if (av->get_handler == NULL
|| (av->flags & NGX_RTMP_VAR_WEAK))
{
break;
}
goto next;
}
}
len = 0;
av = NULL;
for (n = 0; n < cmcf->prefix_variables.nelts; n++) {
if (v[i].name.len >= pv[n].name.len && v[i].name.len > len
&& ngx_strncmp(v[i].name.data, pv[n].name.data, pv[n].name.len)
== 0)
{
av = &pv[n];
len = pv[n].name.len;
}
}
if (av) {
v[i].get_handler = av->get_handler;
v[i].data = (uintptr_t) &v[i].name;
v[i].flags = av->flags;
goto next;
}
if (v[i].get_handler == NULL) {
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"unknown \"%V\" variable", &v[i].name);
return NGX_ERROR;
}
next:
continue;
}
for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {
av = key[n].value;
if (av->flags & NGX_RTMP_VAR_NOHASH) {
key[n].key.data = NULL;
}
}
hash.hash = &cmcf->variables_hash;
hash.key = ngx_hash_key;
hash.max_size = cmcf->variables_hash_max_size;
hash.bucket_size = cmcf->variables_hash_bucket_size;
hash.name = "rtmp_variables_hash";
hash.pool = cf->pool;
hash.temp_pool = NULL;
if (ngx_hash_init(&hash, cmcf->variables_keys->keys.elts,
cmcf->variables_keys->keys.nelts)
!= NGX_OK)
{
return NGX_ERROR;
}
cmcf->variables_keys = NULL;
return NGX_OK;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/mr.z/nginx-http-flv-module.git
[email protected]:mr.z/nginx-http-flv-module.git
mr.z
nginx-http-flv-module
nginx-http-flv-module
master

搜索帮助