Skip to content

Commit 3f08385

Browse files
committed
added common evaluation code
1 parent 9a31ca9 commit 3f08385

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed

ngx_rtmp_eval.c

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Copyright (c) 2012 Roman Arutyunyan
3+
*/
4+
5+
6+
#include "ngx_rtmp_eval.h"
7+
8+
9+
#define NGX_RTMP_EVAL_BUFLEN 16
10+
11+
12+
static void
13+
ngx_rtmp_eval_session_str(ngx_rtmp_session_t *s, ngx_rtmp_eval_t *e,
14+
ngx_str_t *ret)
15+
{
16+
*ret = *(ngx_str_t *) ((u_char *) s + e->offset);
17+
}
18+
19+
20+
static void
21+
ngx_rtmp_eval_connection_str(ngx_rtmp_session_t *s, ngx_rtmp_eval_t *e,
22+
ngx_str_t *ret)
23+
{
24+
*ret = *(ngx_str_t *) ((u_char *) s->connection + e->offset);
25+
}
26+
27+
28+
ngx_rtmp_eval_t ngx_rtmp_eval_session[] = {
29+
30+
{ ngx_string("app"),
31+
ngx_rtmp_eval_session_str,
32+
offsetof(ngx_rtmp_session_t, app) },
33+
34+
{ ngx_string("flashver"),
35+
ngx_rtmp_eval_session_str,
36+
offsetof(ngx_rtmp_session_t, flashver) },
37+
38+
{ ngx_string("swfurl"),
39+
ngx_rtmp_eval_session_str,
40+
offsetof(ngx_rtmp_session_t, swf_url) },
41+
42+
{ ngx_string("tcurl"),
43+
ngx_rtmp_eval_session_str,
44+
offsetof(ngx_rtmp_session_t, tc_url) },
45+
46+
{ ngx_string("pageurl"),
47+
ngx_rtmp_eval_session_str,
48+
offsetof(ngx_rtmp_session_t, page_url) },
49+
50+
{ ngx_string("addr"),
51+
ngx_rtmp_eval_connection_str,
52+
offsetof(ngx_connection_t, addr_text) },
53+
54+
ngx_rtmp_null_eval
55+
};
56+
57+
58+
static void
59+
ngx_rtmp_eval_append(ngx_rtmp_session_t *s, ngx_buf_t *b,
60+
void *data, size_t len)
61+
{
62+
size_t buf_len;
63+
64+
if (b->last + len > b->end) {
65+
buf_len = 2 * (b->last - b->pos) + len;
66+
67+
b->start = ngx_palloc(s->connection->pool, buf_len);
68+
if (b->start == NULL) {
69+
return;
70+
}
71+
72+
b->last = ngx_cpymem(b->start, b->pos, b->last - b->pos);
73+
b->pos = b->start;
74+
b->end = b->start + buf_len;
75+
}
76+
77+
b->last = ngx_cpymem(b->last, data, len);
78+
}
79+
80+
81+
static void
82+
ngx_rtmp_eval_append_var(ngx_rtmp_session_t *s, ngx_buf_t *b,
83+
ngx_rtmp_eval_t *e, ngx_str_t *name)
84+
{
85+
ngx_uint_t k;
86+
ngx_str_t v;
87+
88+
for (k = 0; e->handler; ++k, ++e) {
89+
if (e->name.len == name->len &&
90+
ngx_memcmp(e->name.data, name->data, name->len) == 0)
91+
{
92+
e->handler(s, e, &v);
93+
ngx_rtmp_eval_append(s, b, v.data, v.len);
94+
}
95+
}
96+
}
97+
98+
99+
ngx_int_t
100+
ngx_rtmp_eval(ngx_rtmp_session_t *s, ngx_str_t *in, ngx_rtmp_eval_t *e,
101+
ngx_str_t *out)
102+
{
103+
u_char c, *p;;
104+
ngx_str_t name;
105+
ngx_buf_t b;
106+
ngx_uint_t n;
107+
108+
enum {
109+
NORMAL,
110+
ESCAPE,
111+
NAME,
112+
SNAME
113+
} state = NORMAL;
114+
115+
b.pos = b.last = b.start = ngx_palloc(s->connection->pool,
116+
NGX_RTMP_EVAL_BUFLEN);
117+
if (b.pos == NULL) {
118+
return NGX_ERROR;
119+
}
120+
121+
b.end = b.pos + NGX_RTMP_EVAL_BUFLEN;
122+
123+
for (n = 0; n < in->len; ++n) {
124+
p = &in->data[n];
125+
c = *p;
126+
127+
switch (state) {
128+
case SNAME:
129+
if (c != '}') {
130+
continue;
131+
}
132+
133+
name.len = p - name.data;
134+
ngx_rtmp_eval_append_var(s, &b, e, &name);
135+
136+
state = NORMAL;
137+
138+
continue;
139+
140+
case NAME:
141+
if (c == '{' && name.data == p) {
142+
++name.data;
143+
state = SNAME;
144+
continue;
145+
}
146+
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
147+
continue;
148+
}
149+
150+
name.len = p - name.data;
151+
ngx_rtmp_eval_append_var(s, &b, e, &name);
152+
153+
case NORMAL:
154+
switch (c) {
155+
case '$':
156+
name.data = p + 1;
157+
state = NAME;
158+
continue;
159+
case '\\':
160+
state = ESCAPE;
161+
continue;
162+
}
163+
164+
case ESCAPE:
165+
ngx_rtmp_eval_append(s, &b, &c, 1);
166+
state = NORMAL;
167+
break;
168+
169+
}
170+
}
171+
172+
if (state == NAME) {
173+
name.len = p - name.data;
174+
ngx_rtmp_eval_append_var(s, &b, e, &name);
175+
}
176+
177+
c = 0;
178+
ngx_rtmp_eval_append(s, &b, &c, 1);
179+
180+
out->data = b.pos;
181+
out->len = b.last - b.pos - 1;
182+
183+
return NGX_OK;
184+
}

ngx_rtmp_eval.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2012 Roman Arutyunyan
3+
*/
4+
5+
6+
#ifndef _NGX_RTMP_EVAL_H_INCLUDED_
7+
#define _NGX_RTMP_EVAL_H_INCLUDED_
8+
9+
#include "ngx_rtmp.h"
10+
11+
12+
typedef struct ngx_rtmp_eval_s ngx_rtmp_eval_t;
13+
14+
15+
typedef void (* ngx_rtmp_eval_pt)(ngx_rtmp_session_t *s, ngx_rtmp_eval_t *e,
16+
ngx_str_t *ret);
17+
18+
19+
struct ngx_rtmp_eval_s {
20+
ngx_str_t name;
21+
ngx_rtmp_eval_pt handler;
22+
ngx_uint_t offset;
23+
};
24+
25+
26+
#define ngx_rtmp_null_eval { ngx_null_string, NULL, 0 }
27+
28+
29+
/* standard session eval variables */
30+
extern ngx_rtmp_eval_t ngx_rtmp_eval_session[];
31+
32+
33+
ngx_int_t ngx_rtmp_eval(ngx_rtmp_session_t *s, ngx_str_t *in,
34+
ngx_rtmp_eval_t *e, ngx_str_t *out);
35+
36+
37+
#endif /* _NGX_RTMP_EVAL_H_INCLUDED_ */

0 commit comments

Comments
 (0)