244
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
244
|
5 */
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 static char *ngx_http_flv(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
13
|
|
14 static ngx_command_t ngx_http_flv_commands[] = {
|
|
15
|
|
16 { ngx_string("flv"),
|
|
17 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
|
|
18 ngx_http_flv,
|
|
19 0,
|
|
20 0,
|
|
21 NULL },
|
|
22
|
|
23 ngx_null_command
|
|
24 };
|
|
25
|
|
26
|
650
|
27 static u_char ngx_flv_header[] = "FLV\x1\x5\0\0\0\x9\0\0\0\0";
|
244
|
28
|
|
29
|
|
30 static ngx_http_module_t ngx_http_flv_module_ctx = {
|
|
31 NULL, /* preconfiguration */
|
|
32 NULL, /* postconfiguration */
|
|
33
|
|
34 NULL, /* create main configuration */
|
|
35 NULL, /* init main configuration */
|
|
36
|
|
37 NULL, /* create server configuration */
|
|
38 NULL, /* merge server configuration */
|
|
39
|
|
40 NULL, /* create location configuration */
|
|
41 NULL /* merge location configuration */
|
|
42 };
|
|
43
|
|
44
|
|
45 ngx_module_t ngx_http_flv_module = {
|
|
46 NGX_MODULE_V1,
|
|
47 &ngx_http_flv_module_ctx, /* module context */
|
|
48 ngx_http_flv_commands, /* module directives */
|
|
49 NGX_HTTP_MODULE, /* module type */
|
|
50 NULL, /* init master */
|
|
51 NULL, /* init module */
|
|
52 NULL, /* init process */
|
|
53 NULL, /* init thread */
|
|
54 NULL, /* exit thread */
|
|
55 NULL, /* exit process */
|
|
56 NULL, /* exit master */
|
|
57 NGX_MODULE_V1_PADDING
|
|
58 };
|
|
59
|
|
60
|
|
61 static ngx_int_t
|
|
62 ngx_http_flv_handler(ngx_http_request_t *r)
|
|
63 {
|
432
|
64 u_char *last;
|
244
|
65 off_t start, len;
|
248
|
66 size_t root;
|
244
|
67 ngx_int_t rc;
|
246
|
68 ngx_uint_t level, i;
|
432
|
69 ngx_str_t path, value;
|
244
|
70 ngx_log_t *log;
|
|
71 ngx_buf_t *b;
|
|
72 ngx_chain_t out[2];
|
332
|
73 ngx_open_file_info_t of;
|
244
|
74 ngx_http_core_loc_conf_t *clcf;
|
|
75
|
|
76 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
|
|
77 return NGX_HTTP_NOT_ALLOWED;
|
|
78 }
|
|
79
|
|
80 if (r->uri.data[r->uri.len - 1] == '/') {
|
|
81 return NGX_DECLINED;
|
|
82 }
|
|
83
|
326
|
84 rc = ngx_http_discard_request_body(r);
|
244
|
85
|
326
|
86 if (rc != NGX_OK) {
|
244
|
87 return rc;
|
|
88 }
|
|
89
|
332
|
90 last = ngx_http_map_uri_to_path(r, &path, &root, 0);
|
|
91 if (last == NULL) {
|
244
|
92 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
93 }
|
|
94
|
|
95 log = r->connection->log;
|
|
96
|
332
|
97 path.len = last - path.data;
|
244
|
98
|
332
|
99 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
100 "http flv filename: \"%V\"", &path);
|
|
101
|
|
102 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
244
|
103
|
384
|
104 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
105
|
532
|
106 of.read_ahead = clcf->read_ahead;
|
390
|
107 of.directio = clcf->directio;
|
358
|
108 of.valid = clcf->open_file_cache_valid;
|
|
109 of.min_uses = clcf->open_file_cache_min_uses;
|
332
|
110 of.errors = clcf->open_file_cache_errors;
|
|
111 of.events = clcf->open_file_cache_events;
|
662
|
112 #if (NGX_HAVE_OPENAT)
|
|
113 of.disable_symlinks = clcf->disable_symlinks;
|
|
114 #endif
|
340
|
115
|
362
|
116 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
|
|
117 != NGX_OK)
|
|
118 {
|
332
|
119 switch (of.err) {
|
|
120
|
|
121 case 0:
|
|
122 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
123
|
|
124 case NGX_ENOENT:
|
|
125 case NGX_ENOTDIR:
|
|
126 case NGX_ENAMETOOLONG:
|
|
127
|
244
|
128 level = NGX_LOG_ERR;
|
|
129 rc = NGX_HTTP_NOT_FOUND;
|
332
|
130 break;
|
244
|
131
|
332
|
132 case NGX_EACCES:
|
662
|
133 #if (NGX_HAVE_OPENAT)
|
|
134 case NGX_EMLINK:
|
|
135 case NGX_ELOOP:
|
|
136 #endif
|
332
|
137
|
244
|
138 level = NGX_LOG_ERR;
|
|
139 rc = NGX_HTTP_FORBIDDEN;
|
332
|
140 break;
|
244
|
141
|
332
|
142 default:
|
|
143
|
244
|
144 level = NGX_LOG_CRIT;
|
|
145 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
332
|
146 break;
|
244
|
147 }
|
|
148
|
|
149 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
|
332
|
150 ngx_log_error(level, log, of.err,
|
482
|
151 "%s \"%s\" failed", of.failed, path.data);
|
244
|
152 }
|
|
153
|
|
154 return rc;
|
|
155 }
|
|
156
|
332
|
157 if (!of.is_file) {
|
244
|
158
|
356
|
159 if (ngx_close_file(of.fd) == NGX_FILE_ERROR) {
|
244
|
160 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
161 ngx_close_file_n " \"%s\" failed", path.data);
|
|
162 }
|
|
163
|
|
164 return NGX_DECLINED;
|
|
165 }
|
|
166
|
438
|
167 r->root_tested = !r->error_page;
|
388
|
168
|
244
|
169 start = 0;
|
332
|
170 len = of.size;
|
246
|
171 i = 1;
|
244
|
172
|
|
173 if (r->args.len) {
|
|
174
|
432
|
175 if (ngx_http_arg(r, (u_char *) "start", 5, &value) == NGX_OK) {
|
388
|
176
|
432
|
177 start = ngx_atoof(value.data, value.len);
|
244
|
178
|
|
179 if (start == NGX_ERROR || start >= len) {
|
|
180 start = 0;
|
|
181 }
|
|
182
|
246
|
183 if (start) {
|
|
184 len = sizeof(ngx_flv_header) - 1 + len - start;
|
|
185 i = 0;
|
|
186 }
|
244
|
187 }
|
|
188 }
|
|
189
|
|
190 log->action = "sending flv to client";
|
|
191
|
|
192 r->headers_out.status = NGX_HTTP_OK;
|
246
|
193 r->headers_out.content_length_n = len;
|
332
|
194 r->headers_out.last_modified_time = of.mtime;
|
244
|
195
|
|
196 if (ngx_http_set_content_type(r) != NGX_OK) {
|
|
197 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
198 }
|
|
199
|
246
|
200 if (i == 0) {
|
|
201 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
202 if (b == NULL) {
|
|
203 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
204 }
|
244
|
205
|
246
|
206 b->pos = ngx_flv_header;
|
|
207 b->last = ngx_flv_header + sizeof(ngx_flv_header) - 1;
|
|
208 b->memory = 1;
|
244
|
209
|
246
|
210 out[0].buf = b;
|
|
211 out[0].next = &out[1];
|
|
212 }
|
244
|
213
|
250
|
214
|
244
|
215 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
216 if (b == NULL) {
|
|
217 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
218 }
|
|
219
|
|
220 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
|
|
221 if (b->file == NULL) {
|
|
222 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
223 }
|
|
224
|
390
|
225 r->allow_ranges = 1;
|
|
226
|
244
|
227 rc = ngx_http_send_header(r);
|
|
228
|
|
229 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
230 return rc;
|
|
231 }
|
|
232
|
|
233 b->file_pos = start;
|
332
|
234 b->file_last = of.size;
|
244
|
235
|
|
236 b->in_file = b->file_last ? 1: 0;
|
|
237 b->last_buf = 1;
|
|
238 b->last_in_chain = 1;
|
|
239
|
356
|
240 b->file->fd = of.fd;
|
244
|
241 b->file->name = path;
|
|
242 b->file->log = log;
|
406
|
243 b->file->directio = of.is_directio;
|
244
|
244
|
|
245 out[1].buf = b;
|
|
246 out[1].next = NULL;
|
|
247
|
246
|
248 return ngx_http_output_filter(r, &out[i]);
|
244
|
249 }
|
|
250
|
|
251
|
|
252 static char *
|
|
253 ngx_http_flv(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
254 {
|
|
255 ngx_http_core_loc_conf_t *clcf;
|
|
256
|
|
257 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
258 clcf->handler = ngx_http_flv_handler;
|
|
259
|
|
260 return NGX_CONF_OK;
|
|
261 }
|