Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 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 | { |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 63 | u_char *p, *last; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 64 | off_t start, len; |
Igor Sysoev | d79b21d | 2006-10-12 13:56:16 +0000 | [diff] [blame] | 65 | size_t root; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 66 | ngx_int_t rc; |
Igor Sysoev | a33dafb | 2006-10-11 05:33:15 +0000 | [diff] [blame] | 67 | ngx_uint_t level, i; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 68 | ngx_str_t path; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 69 | ngx_log_t *log; |
| 70 | ngx_buf_t *b; |
| 71 | ngx_chain_t out[2]; |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 72 | ngx_open_file_info_t of; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 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 | |
Igor Sysoev | 832571f | 2007-08-06 15:37:22 +0000 | [diff] [blame] | 88 | rc = ngx_http_discard_request_body(r); |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 89 | |
Igor Sysoev | fda6a08 | 2007-08-07 10:56:09 +0000 | [diff] [blame] | 90 | if (rc != NGX_OK) { |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 91 | return rc; |
| 92 | } |
| 93 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 94 | last = ngx_http_map_uri_to_path(r, &path, &root, 0); |
| 95 | if (last == NULL) { |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 96 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 97 | } |
| 98 | |
| 99 | log = r->connection->log; |
| 100 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 101 | path.len = last - path.data; |
| 102 | |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 103 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 104 | "http flv filename: \"%V\"", &path); |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 105 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 106 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 107 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 108 | of.test_dir = 0; |
Igor Sysoev | 9b9616e | 2007-12-21 16:19:48 +0000 | [diff] [blame] | 109 | of.valid = clcf->open_file_cache_valid; |
Igor Sysoev | f3b0e49 | 2007-12-22 13:19:39 +0000 | [diff] [blame^] | 110 | of.min_uses = clcf->open_file_cache_min_uses; |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 111 | of.errors = clcf->open_file_cache_errors; |
Igor Sysoev | 9afd58f | 2007-09-03 08:41:42 +0000 | [diff] [blame] | 112 | of.events = clcf->open_file_cache_events; |
Igor Sysoev | d3db9ea | 2007-10-09 18:42:00 +0000 | [diff] [blame] | 113 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 114 | rc = ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool); |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 115 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 116 | if (rc == NGX_ERROR) { |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 117 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 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 | |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 127 | level = NGX_LOG_ERR; |
| 128 | rc = NGX_HTTP_NOT_FOUND; |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 129 | break; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 130 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 131 | case NGX_EACCES: |
| 132 | |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 133 | level = NGX_LOG_ERR; |
| 134 | rc = NGX_HTTP_FORBIDDEN; |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 135 | break; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 136 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 137 | default: |
| 138 | |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 139 | level = NGX_LOG_CRIT; |
| 140 | rc = NGX_HTTP_INTERNAL_SERVER_ERROR; |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 141 | break; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 144 | if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) { |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 145 | ngx_log_error(level, log, of.err, |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 146 | ngx_open_file_n " \"%s\" failed", path.data); |
| 147 | } |
| 148 | |
| 149 | return rc; |
| 150 | } |
| 151 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 152 | if (!of.is_file) { |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 153 | |
Igor Sysoev | f4f2efa | 2007-12-07 20:57:38 +0000 | [diff] [blame] | 154 | if (ngx_close_file(of.fd) == NGX_FILE_ERROR) { |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 155 | ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
| 156 | ngx_close_file_n " \"%s\" failed", path.data); |
| 157 | } |
| 158 | |
| 159 | return NGX_DECLINED; |
| 160 | } |
| 161 | |
| 162 | start = 0; |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 163 | len = of.size; |
Igor Sysoev | a33dafb | 2006-10-11 05:33:15 +0000 | [diff] [blame] | 164 | i = 1; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 165 | |
| 166 | if (r->args.len) { |
Igor Sysoev | a8afe40 | 2007-10-01 14:48:45 +0000 | [diff] [blame] | 167 | p = (u_char *) ngx_strnstr(r->args.data, "start=", r->args.len); |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 168 | |
| 169 | if (p) { |
| 170 | p += 6; |
| 171 | |
| 172 | start = ngx_atoof(p, r->args.len - (p - r->args.data)); |
| 173 | |
| 174 | if (start == NGX_ERROR || start >= len) { |
| 175 | start = 0; |
| 176 | } |
| 177 | |
Igor Sysoev | a33dafb | 2006-10-11 05:33:15 +0000 | [diff] [blame] | 178 | if (start) { |
| 179 | len = sizeof(ngx_flv_header) - 1 + len - start; |
| 180 | i = 0; |
| 181 | } |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | log->action = "sending flv to client"; |
| 186 | |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 187 | r->headers_out.status = NGX_HTTP_OK; |
Igor Sysoev | a33dafb | 2006-10-11 05:33:15 +0000 | [diff] [blame] | 188 | r->headers_out.content_length_n = len; |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 189 | r->headers_out.last_modified_time = of.mtime; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 190 | |
| 191 | if (ngx_http_set_content_type(r) != NGX_OK) { |
Igor Sysoev | fc3f068 | 2006-10-09 15:38:59 +0000 | [diff] [blame] | 192 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Igor Sysoev | a33dafb | 2006-10-11 05:33:15 +0000 | [diff] [blame] | 195 | if (i == 0) { |
| 196 | b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); |
| 197 | if (b == NULL) { |
| 198 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 199 | } |
| 200 | |
| 201 | b->pos = ngx_flv_header; |
| 202 | b->last = ngx_flv_header + sizeof(ngx_flv_header) - 1; |
| 203 | b->memory = 1; |
| 204 | |
| 205 | out[0].buf = b; |
| 206 | out[0].next = &out[1]; |
Igor Sysoev | 9ab9f0a | 2006-10-16 07:47:00 +0000 | [diff] [blame] | 207 | |
| 208 | } else { |
Igor Sysoev | 3718891 | 2006-10-17 16:13:49 +0000 | [diff] [blame] | 209 | r->allow_ranges = 1; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Igor Sysoev | 9ab9f0a | 2006-10-16 07:47:00 +0000 | [diff] [blame] | 212 | |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 213 | b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); |
| 214 | if (b == NULL) { |
| 215 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 216 | } |
| 217 | |
| 218 | b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); |
| 219 | if (b->file == NULL) { |
| 220 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 221 | } |
| 222 | |
| 223 | rc = ngx_http_send_header(r); |
| 224 | |
| 225 | if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { |
| 226 | return rc; |
| 227 | } |
| 228 | |
| 229 | b->file_pos = start; |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 230 | b->file_last = of.size; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 231 | |
| 232 | b->in_file = b->file_last ? 1: 0; |
| 233 | b->last_buf = 1; |
| 234 | b->last_in_chain = 1; |
| 235 | |
Igor Sysoev | f4f2efa | 2007-12-07 20:57:38 +0000 | [diff] [blame] | 236 | b->file->fd = of.fd; |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 237 | b->file->name = path; |
| 238 | b->file->log = log; |
| 239 | |
| 240 | out[1].buf = b; |
| 241 | out[1].next = NULL; |
| 242 | |
Igor Sysoev | a33dafb | 2006-10-11 05:33:15 +0000 | [diff] [blame] | 243 | return ngx_http_output_filter(r, &out[i]); |
Igor Sysoev | b505006 | 2006-10-09 14:17:36 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |
| 247 | static char * |
| 248 | ngx_http_flv(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
| 249 | { |
| 250 | ngx_http_core_loc_conf_t *clcf; |
| 251 | |
| 252 | clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); |
| 253 | clcf->handler = ngx_http_flv_handler; |
| 254 | |
| 255 | return NGX_CONF_OK; |
| 256 | } |