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