comparison src/http/modules/ngx_http_flv_module.c @ 244:500a3242dff6 NGINX_0_4_7

nginx 0.4.7 *) Feature: the ngx_http_flv_module. *) Feature: the $request_body_file variable. *) Feature: the "charset" and "source_charset" directives support the variables. *) Bugfix: if an "include" SSI command were before another "include" SSI command with an "wait" parameter, then the "wait" parameter might not work. *) Bugfix: if the "proxy_buffering off" directive was used or while working with memcached the connections might not be closed on timeout. *) Bugfix: nginx did not run on 64-bit platforms except amd64, sparc64, and ppc64.
author Igor Sysoev <http://sysoev.ru>
date Tue, 10 Oct 2006 00:00:00 +0400
parents
children b52bd034c577
comparison
equal deleted inserted replaced
243:d371c9d33781 244:500a3242dff6
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 {
63 u_char *p;
64 off_t start, len;
65 ngx_fd_t fd;
66 ngx_int_t rc;
67 ngx_uint_t level;
68 ngx_str_t path;
69 ngx_err_t err;
70 ngx_log_t *log;
71 ngx_buf_t *b;
72 ngx_chain_t out[2];
73 ngx_file_info_t fi;
74 ngx_pool_cleanup_t *cln;
75 ngx_pool_cleanup_file_t *clnf;
76 ngx_http_core_loc_conf_t *clcf;
77
78 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
79 return NGX_HTTP_NOT_ALLOWED;
80 }
81
82 if (r->uri.data[r->uri.len - 1] == '/') {
83 return NGX_DECLINED;
84 }
85
86 /* TODO: Win32 */
87 if (r->zero_in_uri) {
88 return NGX_DECLINED;
89 }
90
91 rc = ngx_http_discard_body(r);
92
93 if (rc != NGX_OK && rc != NGX_AGAIN) {
94 return rc;
95 }
96
97 if (ngx_http_map_uri_to_path(r, &path, 0) == NULL) {
98 return NGX_HTTP_INTERNAL_SERVER_ERROR;
99 }
100
101 log = r->connection->log;
102
103 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
104 "http flv filename: \"%s\"", path.data);
105
106 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t));
107 if (cln == NULL) {
108 return NGX_HTTP_INTERNAL_SERVER_ERROR;
109 }
110
111 fd = ngx_open_file(path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
112
113 if (fd == NGX_INVALID_FILE) {
114 err = ngx_errno;
115
116 if (err == NGX_ENOENT
117 || err == NGX_ENOTDIR
118 || err == NGX_ENAMETOOLONG)
119 {
120 level = NGX_LOG_ERR;
121 rc = NGX_HTTP_NOT_FOUND;
122
123 } else if (err == NGX_EACCES) {
124 level = NGX_LOG_ERR;
125 rc = NGX_HTTP_FORBIDDEN;
126
127 } else {
128 level = NGX_LOG_CRIT;
129 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
130 }
131
132 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
133
134 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
135 ngx_log_error(level, log, err,
136 ngx_open_file_n " \"%s\" failed", path.data);
137 }
138
139 return rc;
140 }
141
142 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
143 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
144 ngx_fd_info_n " \"%s\" failed", path.data);
145
146 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
147 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
148 ngx_close_file_n " \"%s\" failed", path.data);
149 }
150
151 return NGX_HTTP_INTERNAL_SERVER_ERROR;
152 }
153
154 if (!ngx_is_file(&fi)) {
155
156 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
157 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
158 ngx_close_file_n " \"%s\" failed", path.data);
159 }
160
161 return NGX_DECLINED;
162 }
163
164 start = 0;
165 len = ngx_file_size(&fi);
166
167 if (r->args.len) {
168 p = (u_char *) ngx_strstr(r->args.data, "start=");
169
170 if (p) {
171 p += 6;
172
173 start = ngx_atoof(p, r->args.len - (p - r->args.data));
174
175 if (start == NGX_ERROR || start >= len) {
176 start = 0;
177 }
178
179 len -= start;
180 }
181 }
182
183 log->action = "sending flv to client";
184
185 cln->handler = ngx_pool_cleanup_file;
186 clnf = cln->data;
187
188 clnf->fd = fd;
189 clnf->name = path.data;
190 clnf->log = r->pool->log;
191
192 r->headers_out.status = NGX_HTTP_OK;
193 r->headers_out.content_length_n = sizeof(ngx_flv_header) - 1 + len;
194 r->headers_out.last_modified_time = ngx_file_mtime(&fi);
195
196 if (ngx_http_set_content_type(r) != NGX_OK) {
197 return NGX_HTTP_INTERNAL_SERVER_ERROR;
198 }
199
200 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
201 if (b == NULL) {
202 return NGX_HTTP_INTERNAL_SERVER_ERROR;
203 }
204
205 b->pos = ngx_flv_header;
206 b->last = ngx_flv_header + sizeof(ngx_flv_header) - 1;
207 b->memory = 1;
208
209 out[0].buf = b;
210 out[0].next = &out[1];
211
212 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
213 if (b == NULL) {
214 return NGX_HTTP_INTERNAL_SERVER_ERROR;
215 }
216
217 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
218 if (b->file == NULL) {
219 return NGX_HTTP_INTERNAL_SERVER_ERROR;
220 }
221
222 rc = ngx_http_send_header(r);
223
224 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
225 return rc;
226 }
227
228 b->file_pos = start;
229 b->file_last = ngx_file_size(&fi);
230
231 b->in_file = b->file_last ? 1: 0;
232 b->last_buf = 1;
233 b->last_in_chain = 1;
234
235 b->file->fd = fd;
236 b->file->name = path;
237 b->file->log = log;
238
239 out[1].buf = b;
240 out[1].next = NULL;
241
242 return ngx_http_output_filter(r, out);
243 }
244
245
246 static char *
247 ngx_http_flv(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
248 {
249 ngx_http_core_loc_conf_t *clcf;
250
251 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
252 clcf->handler = ngx_http_flv_handler;
253
254 return NGX_CONF_OK;
255 }