3 * Copyright (C) Igor Sysoev
6 #include <ngx_config.h>
11 static char *ngx_http_flv(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
13 static ngx_command_t ngx_http_flv_commands[] = {
16 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
26 static u_char ngx_flv_header[] = "FLV\x1\x1\0\0\0\x9\0\0\0\x9";
29 static ngx_http_module_t ngx_http_flv_module_ctx = {
30 NULL, /* preconfiguration */
31 NULL, /* postconfiguration */
33 NULL, /* create main configuration */
34 NULL, /* init main configuration */
36 NULL, /* create server configuration */
37 NULL, /* merge server configuration */
39 NULL, /* create location configuration */
40 NULL /* merge location configuration */
44 ngx_module_t ngx_http_flv_module = {
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 */
61 ngx_http_flv_handler(ngx_http_request_t *r)
68 ngx_str_t path, value;
72 ngx_open_file_info_t of;
73 ngx_http_core_loc_conf_t *clcf;
75 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
76 return NGX_HTTP_NOT_ALLOWED;
79 if (r->uri.data[r->uri.len - 1] == '/') {
88 rc = ngx_http_discard_request_body(r);
94 last = ngx_http_map_uri_to_path(r, &path, &root, 0);
96 return NGX_HTTP_INTERNAL_SERVER_ERROR;
99 log = r->connection->log;
101 path.len = last - path.data;
103 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
104 "http flv filename: \"%V\"", &path);
106 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
108 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
110 of.directio = clcf->directio;
111 of.valid = clcf->open_file_cache_valid;
112 of.min_uses = clcf->open_file_cache_min_uses;
113 of.errors = clcf->open_file_cache_errors;
114 of.events = clcf->open_file_cache_events;
116 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
122 return NGX_HTTP_INTERNAL_SERVER_ERROR;
126 case NGX_ENAMETOOLONG:
129 rc = NGX_HTTP_NOT_FOUND;
135 rc = NGX_HTTP_FORBIDDEN;
140 level = NGX_LOG_CRIT;
141 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
145 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
146 ngx_log_error(level, log, of.err,
147 ngx_open_file_n " \"%s\" failed", path.data);
155 if (ngx_close_file(of.fd) == NGX_FILE_ERROR) {
156 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
157 ngx_close_file_n " \"%s\" failed", path.data);
163 r->root_tested = !r->error_page;
171 if (ngx_http_arg(r, (u_char *) "start", 5, &value) == NGX_OK) {
173 start = ngx_atoof(value.data, value.len);
175 if (start == NGX_ERROR || start >= len) {
180 len = sizeof(ngx_flv_header) - 1 + len - start;
186 log->action = "sending flv to client";
188 r->headers_out.status = NGX_HTTP_OK;
189 r->headers_out.content_length_n = len;
190 r->headers_out.last_modified_time = of.mtime;
192 if (ngx_http_set_content_type(r) != NGX_OK) {
193 return NGX_HTTP_INTERNAL_SERVER_ERROR;
197 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
199 return NGX_HTTP_INTERNAL_SERVER_ERROR;
202 b->pos = ngx_flv_header;
203 b->last = ngx_flv_header + sizeof(ngx_flv_header) - 1;
207 out[0].next = &out[1];
211 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
213 return NGX_HTTP_INTERNAL_SERVER_ERROR;
216 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
217 if (b->file == NULL) {
218 return NGX_HTTP_INTERNAL_SERVER_ERROR;
223 rc = ngx_http_send_header(r);
225 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
230 b->file_last = of.size;
232 b->in_file = b->file_last ? 1: 0;
234 b->last_in_chain = 1;
237 b->file->name = path;
239 b->file->directio = of.is_directio;
244 return ngx_http_output_filter(r, &out[i]);
249 ngx_http_flv(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
251 ngx_http_core_loc_conf_t *clcf;
253 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
254 clcf->handler = ngx_http_flv_handler;