Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_flv_module.c @ 1822:935427c55e04
test URI/Destination collection/non-collection and Depth
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 03 Jan 2008 20:09:51 +0000 |
parents | 0b5b94805d26 |
children | 67a29af877ed 81f4bf441eb0 |
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_int_t rc; |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
67 ngx_uint_t level, i; |
753 | 68 ngx_str_t path; |
69 ngx_log_t *log; | |
70 ngx_buf_t *b; | |
71 ngx_chain_t out[2]; | |
1454 | 72 ngx_open_file_info_t of; |
753 | 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 | |
83 /* TODO: Win32 */ | |
84 if (r->zero_in_uri) { | |
85 return NGX_DECLINED; | |
86 } | |
87 | |
1370
cc114c85be0f
rename ngx_http_discard_body() to ngx_http_discard_request_body()
Igor Sysoev <igor@sysoev.ru>
parents:
1048
diff
changeset
|
88 rc = ngx_http_discard_request_body(r); |
753 | 89 |
1374 | 90 if (rc != NGX_OK) { |
753 | 91 return rc; |
92 } | |
93 | |
1454 | 94 last = ngx_http_map_uri_to_path(r, &path, &root, 0); |
95 if (last == NULL) { | |
753 | 96 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
97 } | |
98 | |
99 log = r->connection->log; | |
100 | |
1454 | 101 path.len = last - path.data; |
753 | 102 |
1454 | 103 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, |
104 "http flv filename: \"%V\"", &path); | |
105 | |
106 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
753 | 107 |
1454 | 108 of.test_dir = 0; |
1767
c42431762903
open_file_cache_retest > open_file_cache_valid
Igor Sysoev <igor@sysoev.ru>
parents:
1696
diff
changeset
|
109 of.valid = clcf->open_file_cache_valid; |
1772 | 110 of.min_uses = clcf->open_file_cache_min_uses; |
1454 | 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 |
1799 | 114 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool) |
115 != NGX_OK) | |
116 { | |
1454 | 117 switch (of.err) { |
118 | |
119 case 0: | |
120 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
121 | |
122 case NGX_ENOENT: | |
123 case NGX_ENOTDIR: | |
124 case NGX_ENAMETOOLONG: | |
125 | |
753 | 126 level = NGX_LOG_ERR; |
127 rc = NGX_HTTP_NOT_FOUND; | |
1454 | 128 break; |
753 | 129 |
1454 | 130 case NGX_EACCES: |
131 | |
753 | 132 level = NGX_LOG_ERR; |
133 rc = NGX_HTTP_FORBIDDEN; | |
1454 | 134 break; |
753 | 135 |
1454 | 136 default: |
137 | |
753 | 138 level = NGX_LOG_CRIT; |
139 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1454 | 140 break; |
753 | 141 } |
142 | |
143 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) { | |
1454 | 144 ngx_log_error(level, log, of.err, |
753 | 145 ngx_open_file_n " \"%s\" failed", path.data); |
146 } | |
147 | |
148 return rc; | |
149 } | |
150 | |
1454 | 151 if (!of.is_file) { |
753 | 152 |
1696 | 153 if (ngx_close_file(of.fd) == NGX_FILE_ERROR) { |
753 | 154 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
155 ngx_close_file_n " \"%s\" failed", path.data); | |
156 } | |
157 | |
158 return NGX_DECLINED; | |
159 } | |
160 | |
161 start = 0; | |
1454 | 162 len = of.size; |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
163 i = 1; |
753 | 164 |
165 if (r->args.len) { | |
1553 | 166 p = (u_char *) ngx_strnstr(r->args.data, "start=", r->args.len); |
753 | 167 |
168 if (p) { | |
169 p += 6; | |
170 | |
171 start = ngx_atoof(p, r->args.len - (p - r->args.data)); | |
172 | |
173 if (start == NGX_ERROR || start >= len) { | |
174 start = 0; | |
175 } | |
176 | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
177 if (start) { |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
178 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
|
179 i = 0; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
180 } |
753 | 181 } |
182 } | |
183 | |
184 log->action = "sending flv to client"; | |
185 | |
186 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
|
187 r->headers_out.content_length_n = len; |
1454 | 188 r->headers_out.last_modified_time = of.mtime; |
753 | 189 |
190 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
|
191 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
753 | 192 } |
193 | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
194 if (i == 0) { |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
195 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
|
196 if (b == NULL) { |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
197 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
198 } |
753 | 199 |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
200 b->pos = ngx_flv_header; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
201 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
|
202 b->memory = 1; |
753 | 203 |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
204 out[0].buf = b; |
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
205 out[0].next = &out[1]; |
781
836f099aa5cb
allow ranges for full flv files
Igor Sysoev <igor@sysoev.ru>
parents:
774
diff
changeset
|
206 |
836f099aa5cb
allow ranges for full flv files
Igor Sysoev <igor@sysoev.ru>
parents:
774
diff
changeset
|
207 } else { |
787
b310630d129e
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
781
diff
changeset
|
208 r->allow_ranges = 1; |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
209 } |
753 | 210 |
781
836f099aa5cb
allow ranges for full flv files
Igor Sysoev <igor@sysoev.ru>
parents:
774
diff
changeset
|
211 |
753 | 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; | |
1454 | 229 b->file_last = of.size; |
753 | 230 |
231 b->in_file = b->file_last ? 1: 0; | |
232 b->last_buf = 1; | |
233 b->last_in_chain = 1; | |
234 | |
1696 | 235 b->file->fd = of.fd; |
753 | 236 b->file->name = path; |
237 b->file->log = log; | |
238 | |
239 out[1].buf = b; | |
240 out[1].next = NULL; | |
241 | |
764
761a94247118
do not send flv header for full file
Igor Sysoev <igor@sysoev.ru>
parents:
754
diff
changeset
|
242 return ngx_http_output_filter(r, &out[i]); |
753 | 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 } |