forked from arut/nginx-rtmp-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_rtmp_eval.c
282 lines (210 loc) · 5.92 KB
/
ngx_rtmp_eval.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright (C) Roman Arutyunyan
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include "ngx_rtmp_eval.h"
#define NGX_RTMP_EVAL_BUFLEN 16
static void
ngx_rtmp_eval_session_str(void *ctx, ngx_rtmp_eval_t *e, ngx_str_t *ret)
{
*ret = *(ngx_str_t *) ((u_char *) ctx + e->offset);
}
static void
ngx_rtmp_eval_connection_str(void *ctx, ngx_rtmp_eval_t *e, ngx_str_t *ret)
{
ngx_rtmp_session_t *s = ctx;
*ret = *(ngx_str_t *) ((u_char *) s->connection + e->offset);
}
ngx_rtmp_eval_t ngx_rtmp_eval_session[] = {
{ ngx_string("app"),
ngx_rtmp_eval_session_str,
offsetof(ngx_rtmp_session_t, app) },
{ ngx_string("flashver"),
ngx_rtmp_eval_session_str,
offsetof(ngx_rtmp_session_t, flashver) },
{ ngx_string("swfurl"),
ngx_rtmp_eval_session_str,
offsetof(ngx_rtmp_session_t, swf_url) },
{ ngx_string("tcurl"),
ngx_rtmp_eval_session_str,
offsetof(ngx_rtmp_session_t, tc_url) },
{ ngx_string("pageurl"),
ngx_rtmp_eval_session_str,
offsetof(ngx_rtmp_session_t, page_url) },
{ ngx_string("addr"),
ngx_rtmp_eval_connection_str,
offsetof(ngx_connection_t, addr_text) },
ngx_rtmp_null_eval
};
static void
ngx_rtmp_eval_append(ngx_buf_t *b, void *data, size_t len, ngx_log_t *log)
{
size_t buf_len;
if (b->last + len > b->end) {
buf_len = 2 * (b->last - b->pos) + len;
b->start = ngx_alloc(buf_len, log);
if (b->start == NULL) {
return;
}
b->last = ngx_cpymem(b->start, b->pos, b->last - b->pos);
b->pos = b->start;
b->end = b->start + buf_len;
}
b->last = ngx_cpymem(b->last, data, len);
}
static void
ngx_rtmp_eval_append_var(void *ctx, ngx_buf_t *b, ngx_rtmp_eval_t **e,
ngx_str_t *name, ngx_log_t *log)
{
ngx_uint_t k;
ngx_str_t v;
ngx_rtmp_eval_t *ee;
for (; *e; ++e) {
for (k = 0, ee = *e; ee->handler; ++k, ++ee) {
if (ee->name.len == name->len &&
ngx_memcmp(ee->name.data, name->data, name->len) == 0)
{
ee->handler(ctx, ee, &v);
ngx_rtmp_eval_append(b, v.data, v.len, log);
}
}
}
}
ngx_int_t
ngx_rtmp_eval(void *ctx, ngx_str_t *in, ngx_rtmp_eval_t **e, ngx_str_t *out,
ngx_log_t *log)
{
u_char c, *p;
ngx_str_t name;
ngx_buf_t b;
ngx_uint_t n;
enum {
NORMAL,
ESCAPE,
NAME,
SNAME
} state = NORMAL;
b.pos = b.last = b.start = ngx_alloc(NGX_RTMP_EVAL_BUFLEN, log);
if (b.pos == NULL) {
return NGX_ERROR;
}
b.end = b.pos + NGX_RTMP_EVAL_BUFLEN;
name.data = NULL;
for (n = 0; n < in->len; ++n) {
p = &in->data[n];
c = *p;
switch (state) {
case SNAME:
if (c != '}') {
continue;
}
name.len = p - name.data;
ngx_rtmp_eval_append_var(ctx, &b, e, &name, log);
state = NORMAL;
continue;
case NAME:
if (c == '{' && name.data == p) {
++name.data;
state = SNAME;
continue;
}
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
continue;
}
name.len = p - name.data;
ngx_rtmp_eval_append_var(ctx, &b, e, &name, log);
case NORMAL:
switch (c) {
case '$':
name.data = p + 1;
state = NAME;
continue;
case '\\':
state = ESCAPE;
continue;
}
case ESCAPE:
ngx_rtmp_eval_append(&b, &c, 1, log);
state = NORMAL;
break;
}
}
if (state == NAME) {
p = &in->data[n];
name.len = p - name.data;
ngx_rtmp_eval_append_var(ctx, &b, e, &name, log);
}
c = 0;
ngx_rtmp_eval_append(&b, &c, 1, log);
out->data = b.pos;
out->len = b.last - b.pos - 1;
return NGX_OK;
}
ngx_int_t
ngx_rtmp_eval_streams(ngx_str_t *in)
{
#if !(NGX_WIN32)
ngx_int_t mode, create, v, close_src;
ngx_fd_t dst, src;
u_char *path;
path = in->data;
while (*path >= '0' && *path <= '9') {
path++;
}
switch ((char) *path) {
case '>':
v = (path == in->data ? 1 : ngx_atoi(in->data, path - in->data));
if (v == NGX_ERROR) {
return NGX_ERROR;
}
dst = (ngx_fd_t) v;
mode = NGX_FILE_WRONLY;
create = NGX_FILE_TRUNCATE;
path++;
if (*path == (u_char) '>') {
mode = NGX_FILE_APPEND;
create = NGX_FILE_CREATE_OR_OPEN;
path++;
}
break;
case '<':
v = (path == in->data ? 0 : ngx_atoi(in->data, path - in->data));
if (v == NGX_ERROR) {
return NGX_ERROR;
}
dst = (ngx_fd_t) v;
mode = NGX_FILE_RDONLY;
create = NGX_FILE_OPEN;
path++;
break;
default:
return NGX_DONE;
}
if (*path == (u_char) '&') {
path++;
v = ngx_atoi(path, in->data + in->len - path);
if (v == NGX_ERROR) {
return NGX_ERROR;
}
src = (ngx_fd_t) v;
close_src = 0;
} else {
src = ngx_open_file(path, mode, create, NGX_FILE_DEFAULT_ACCESS);
if (src == NGX_INVALID_FILE) {
return NGX_ERROR;
}
close_src = 1;
}
if (src == dst) {
return NGX_OK;
}
dup2(src, dst);
if (close_src) {
ngx_close_file(src);
}
return NGX_OK;
#else
return NGX_DONE;
#endif
}