Mercurial > hg > nginx
annotate src/http/modules/ngx_http_flv_module.c @ 1560:25ee6eee7573
style fix: remove trailing spaces
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 09 Oct 2007 18:42:00 +0000 |
parents | e3134867a810 |
children | f5f124fbfa3b |
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 { | |
1454 | 63 u_char *p, *last; |
753 | 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_log_t *log; | |
71 ngx_buf_t *b; | |
72 ngx_chain_t out[2]; | |
1454 | 73 ngx_open_file_info_t of; |
753 | 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 | |
84 /* TODO: Win32 */ | |
85 if (r->zero_in_uri) { | |
86 return NGX_DECLINED; | |
87 } | |
88 | |
1370
cc114c85be0f
rename ngx_http_discard_body() to ngx_http_discard_request_body()
Igor Sysoev <igor@sysoev.ru>
parents:
1048
diff
changeset
|
89 rc = ngx_http_discard_request_body(r); |
753 | 90 |
1374 | 91 if (rc != NGX_OK) { |
753 | 92 return rc; |
93 } | |
94 | |
1454 | 95 last = ngx_http_map_uri_to_path(r, &path, &root, 0); |
96 if (last == NULL) { | |
753 | 97 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
98 } | |
99 | |
100 log = r->connection->log; | |
101 | |
1454 | 102 path.len = last - path.data; |
753 | 103 |
1454 | 104 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, |
105 "http flv filename: \"%V\"", &path); | |
106 | |
107 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
753 | 108 |
1454 | 109 of.test_dir = 0; |
110 of.retest = clcf->open_file_cache_retest; | |
111 of.errors = clcf->open_file_cache_errors; | |
1457 | 112 of.events = clcf->open_file_cache_events; |
1560
25ee6eee7573
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1553
diff
changeset
|
113 |
1454 | 114 rc = ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool); |
753 | 115 |
1454 | 116 if (rc == NGX_ERROR) { |
753 | 117 |
1454 | 118 switch (of.err) { |
119 | |
120 case 0: | |
121 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
122 | |
123 case NGX_ENOENT: | |
124 case NGX_ENOTDIR: | |
125 case NGX_ENAMETOOLONG: | |
126 | |
753 | 127 level = NGX_LOG_ERR; |
128 rc = NGX_HTTP_NOT_FOUND; | |
1454 | 129 break; |
753 | 130 |
1454 | 131 case NGX_EACCES: |
132 | |
753 | 133 level = NGX_LOG_ERR; |
134 rc = NGX_HTTP_FORBIDDEN; | |
1454 | 135 break; |
753 | 136 |
1454 | 137 default: |
138 | |
753 | 139 level = NGX_LOG_CRIT; |
140 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1454 | 141 break; |
753 | 142 } |
143 | |
144 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) { | |
1454 | 145 ngx_log_error(level, log, of.err, |
753 | 146 ngx_open_file_n " \"%s\" failed", path.data); |
147 } | |
148 | |
149 return rc; | |
150 } | |
151 | |
1454 | 152 fd = of.fd; |
753 | 153 |
1454 | 154 if (!of.is_file) { |
753 | 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; | |
1454 | 165 len = of.size; |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
166 i = 1; |
753 | 167 |
168 if (r->args.len) { | |
1553 | 169 p = (u_char *) ngx_strnstr(r->args.data, "start=", r->args.len); |
753 | 170 |
171 if (p) { | |
172 p += 6; | |
173 | |
174 start = ngx_atoof(p, r->args.len - (p - r->args.data)); | |
175 | |
176 if (start == NGX_ERROR || start >= len) { | |
177 start = 0; | |
178 } | |
179 | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
180 if (start) { |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
181 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
|
182 i = 0; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
183 } |
753 | 184 } |
185 } | |
186 | |
187 log->action = "sending flv to client"; | |
188 | |
189 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
|
190 r->headers_out.content_length_n = len; |
1454 | 191 r->headers_out.last_modified_time = of.mtime; |
753 | 192 |
193 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
|
194 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
753 | 195 } |
196 | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
197 if (i == 0) { |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
198 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
|
199 if (b == NULL) { |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
200 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
201 } |
753 | 202 |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
203 b->pos = ngx_flv_header; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
204 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
|
205 b->memory = 1; |
753 | 206 |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
207 out[0].buf = b; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
208 out[0].next = &out[1]; |
781
836f099aa5cb
allow ranges for full flv files
Igor Sysoev <igor@sysoev.ru>
parents:
774
diff
changeset
|
209 |
836f099aa5cb
allow ranges for full flv files
Igor Sysoev <igor@sysoev.ru>
parents:
774
diff
changeset
|
210 } else { |
787
b310630d129e
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
781
diff
changeset
|
211 r->allow_ranges = 1; |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
212 } |
753 | 213 |
781
836f099aa5cb
allow ranges for full flv files
Igor Sysoev <igor@sysoev.ru>
parents:
774
diff
changeset
|
214 |
753 | 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 | |
225 rc = ngx_http_send_header(r); | |
226 | |
227 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
228 return rc; | |
229 } | |
230 | |
231 b->file_pos = start; | |
1454 | 232 b->file_last = of.size; |
753 | 233 |
234 b->in_file = b->file_last ? 1: 0; | |
235 b->last_buf = 1; | |
236 b->last_in_chain = 1; | |
237 | |
238 b->file->fd = fd; | |
239 b->file->name = path; | |
240 b->file->log = log; | |
241 | |
242 out[1].buf = b; | |
243 out[1].next = NULL; | |
244 | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
245 return ngx_http_output_filter(r, &out[i]); |
753 | 246 } |
247 | |
248 | |
249 static char * | |
250 ngx_http_flv(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
251 { | |
252 ngx_http_core_loc_conf_t *clcf; | |
253 | |
254 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
255 clcf->handler = ngx_http_flv_handler; | |
256 | |
257 return NGX_CONF_OK; | |
258 } |