Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_flv_module.c @ 1204:94774fc6620f
style fix
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 17 May 2007 18:07:28 +0000 |
parents | c4f666fc3a7e |
children | cc114c85be0f ca6845b19113 |
rev | line source |
---|---|
753 | 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; | |
774
589841f06b87
previous commit broke two modules
Igor Sysoev <igor@sysoev.ru>
parents:
764
diff
changeset
|
65 size_t root; |
753 | 66 ngx_fd_t fd; |
67 ngx_int_t rc; | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
68 ngx_uint_t level, i; |
753 | 69 ngx_str_t path; |
70 ngx_err_t err; | |
71 ngx_log_t *log; | |
72 ngx_buf_t *b; | |
73 ngx_chain_t out[2]; | |
74 ngx_file_info_t fi; | |
75 ngx_pool_cleanup_t *cln; | |
76 ngx_pool_cleanup_file_t *clnf; | |
77 ngx_http_core_loc_conf_t *clcf; | |
78 | |
79 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { | |
80 return NGX_HTTP_NOT_ALLOWED; | |
81 } | |
82 | |
83 if (r->uri.data[r->uri.len - 1] == '/') { | |
84 return NGX_DECLINED; | |
85 } | |
86 | |
87 /* TODO: Win32 */ | |
88 if (r->zero_in_uri) { | |
89 return NGX_DECLINED; | |
90 } | |
91 | |
92 rc = ngx_http_discard_body(r); | |
93 | |
94 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
95 return rc; | |
96 } | |
97 | |
774
589841f06b87
previous commit broke two modules
Igor Sysoev <igor@sysoev.ru>
parents:
764
diff
changeset
|
98 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) { |
753 | 99 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
100 } | |
101 | |
102 log = r->connection->log; | |
103 | |
104 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, | |
105 "http flv filename: \"%s\"", path.data); | |
106 | |
107 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t)); | |
108 if (cln == NULL) { | |
109 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
110 } | |
111 | |
1048
c4f666fc3a7e
ngx_open_file(name, access, create) > ngx_open_file(name, mode, create, access)
Igor Sysoev <igor@sysoev.ru>
parents:
787
diff
changeset
|
112 fd = ngx_open_file(path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); |
753 | 113 |
114 if (fd == NGX_INVALID_FILE) { | |
115 err = ngx_errno; | |
116 | |
117 if (err == NGX_ENOENT | |
118 || err == NGX_ENOTDIR | |
119 || err == NGX_ENAMETOOLONG) | |
120 { | |
121 level = NGX_LOG_ERR; | |
122 rc = NGX_HTTP_NOT_FOUND; | |
123 | |
124 } else if (err == NGX_EACCES) { | |
125 level = NGX_LOG_ERR; | |
126 rc = NGX_HTTP_FORBIDDEN; | |
127 | |
128 } else { | |
129 level = NGX_LOG_CRIT; | |
130 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
131 } | |
132 | |
133 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
134 | |
135 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) { | |
136 ngx_log_error(level, log, err, | |
137 ngx_open_file_n " \"%s\" failed", path.data); | |
138 } | |
139 | |
140 return rc; | |
141 } | |
142 | |
143 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { | |
144 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, | |
145 ngx_fd_info_n " \"%s\" failed", path.data); | |
146 | |
147 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
148 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
149 ngx_close_file_n " \"%s\" failed", path.data); | |
150 } | |
151 | |
152 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
153 } | |
154 | |
155 if (!ngx_is_file(&fi)) { | |
156 | |
157 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
158 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
159 ngx_close_file_n " \"%s\" failed", path.data); | |
160 } | |
161 | |
162 return NGX_DECLINED; | |
163 } | |
164 | |
165 start = 0; | |
166 len = ngx_file_size(&fi); | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
167 i = 1; |
753 | 168 |
169 if (r->args.len) { | |
170 p = (u_char *) ngx_strstr(r->args.data, "start="); | |
171 | |
172 if (p) { | |
173 p += 6; | |
174 | |
175 start = ngx_atoof(p, r->args.len - (p - r->args.data)); | |
176 | |
177 if (start == NGX_ERROR || start >= len) { | |
178 start = 0; | |
179 } | |
180 | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
181 if (start) { |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
182 len = sizeof(ngx_flv_header) - 1 + len - start; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
183 i = 0; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
184 } |
753 | 185 } |
186 } | |
187 | |
188 log->action = "sending flv to client"; | |
189 | |
190 cln->handler = ngx_pool_cleanup_file; | |
754
4ac89c5aa10d
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
753
diff
changeset
|
191 clnf = cln->data; |
753 | 192 |
193 clnf->fd = fd; | |
194 clnf->name = path.data; | |
195 clnf->log = r->pool->log; | |
196 | |
197 r->headers_out.status = NGX_HTTP_OK; | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
198 r->headers_out.content_length_n = len; |
753 | 199 r->headers_out.last_modified_time = ngx_file_mtime(&fi); |
200 | |
201 if (ngx_http_set_content_type(r) != NGX_OK) { | |
754
4ac89c5aa10d
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
753
diff
changeset
|
202 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
753 | 203 } |
204 | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
205 if (i == 0) { |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
206 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
207 if (b == NULL) { |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
208 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
209 } |
753 | 210 |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
211 b->pos = ngx_flv_header; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
212 b->last = ngx_flv_header + sizeof(ngx_flv_header) - 1; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
213 b->memory = 1; |
753 | 214 |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
215 out[0].buf = b; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
216 out[0].next = &out[1]; |
781
836f099aa5cb
allow ranges for full flv files
Igor Sysoev <igor@sysoev.ru>
parents:
774
diff
changeset
|
217 |
836f099aa5cb
allow ranges for full flv files
Igor Sysoev <igor@sysoev.ru>
parents:
774
diff
changeset
|
218 } else { |
787
b310630d129e
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
781
diff
changeset
|
219 r->allow_ranges = 1; |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
220 } |
753 | 221 |
781
836f099aa5cb
allow ranges for full flv files
Igor Sysoev <igor@sysoev.ru>
parents:
774
diff
changeset
|
222 |
753 | 223 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); |
224 if (b == NULL) { | |
225 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
226 } | |
227 | |
228 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); | |
229 if (b->file == NULL) { | |
230 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
231 } | |
232 | |
233 rc = ngx_http_send_header(r); | |
234 | |
235 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
236 return rc; | |
237 } | |
238 | |
239 b->file_pos = start; | |
240 b->file_last = ngx_file_size(&fi); | |
241 | |
242 b->in_file = b->file_last ? 1: 0; | |
243 b->last_buf = 1; | |
244 b->last_in_chain = 1; | |
245 | |
246 b->file->fd = fd; | |
247 b->file->name = path; | |
248 b->file->log = log; | |
249 | |
250 out[1].buf = b; | |
251 out[1].next = NULL; | |
252 | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
253 return ngx_http_output_filter(r, &out[i]); |
753 | 254 } |
255 | |
256 | |
257 static char * | |
258 ngx_http_flv(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
259 { | |
260 ngx_http_core_loc_conf_t *clcf; | |
261 | |
262 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
263 clcf->handler = ngx_http_flv_handler; | |
264 | |
265 return NGX_CONF_OK; | |
266 } |