7 #include "http_chunk.h"
14 /* plugin config for all request/connections */
26 plugin_config
**config_storage
;
31 /* init the plugin data */
32 INIT_FUNC(mod_flv_streaming_init
) {
35 p
= calloc(1, sizeof(*p
));
37 p
->query_str
= buffer_init();
38 p
->get_params
= array_init();
43 /* detroy the plugin data */
44 FREE_FUNC(mod_flv_streaming_free
) {
49 if (!p
) return HANDLER_GO_ON
;
51 if (p
->config_storage
) {
54 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
55 plugin_config
*s
= p
->config_storage
[i
];
57 if (NULL
== s
) continue;
59 array_free(s
->extensions
);
63 free(p
->config_storage
);
66 buffer_free(p
->query_str
);
67 array_free(p
->get_params
);
74 /* handle plugin config and check values */
76 SETDEFAULTS_FUNC(mod_flv_streaming_set_defaults
) {
80 config_values_t cv
[] = {
81 { "flv-streaming.extensions", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
82 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
85 if (!p
) return HANDLER_ERROR
;
87 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
89 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
90 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
93 s
= calloc(1, sizeof(plugin_config
));
94 s
->extensions
= array_init();
96 cv
[0].destination
= s
->extensions
;
98 p
->config_storage
[i
] = s
;
100 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
101 return HANDLER_ERROR
;
104 if (!array_is_vlist(s
->extensions
)) {
105 log_error_write(srv
, __FILE__
, __LINE__
, "s",
106 "unexpected value for flv-streaming.extensions; expected list of \"ext\"");
107 return HANDLER_ERROR
;
111 return HANDLER_GO_ON
;
116 static int mod_flv_streaming_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
118 plugin_config
*s
= p
->config_storage
[0];
122 /* skip the first, the global context */
123 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
124 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
125 s
= p
->config_storage
[i
];
127 /* condition didn't match */
128 if (!config_check_cond(srv
, con
, dc
)) continue;
131 for (j
= 0; j
< dc
->value
->used
; j
++) {
132 data_unset
*du
= dc
->value
->data
[j
];
134 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("flv-streaming.extensions"))) {
144 static int split_get_params(array
*get_params
, buffer
*qrystr
) {
147 char *key
= NULL
, *val
= NULL
;
152 len
= buffer_string_length(qrystr
);
153 for (i
= 0; i
<= len
; i
++) {
154 switch(qrystr
->ptr
[i
]) {
157 val
= qrystr
->ptr
+ i
+ 1;
159 qrystr
->ptr
[i
] = '\0';
166 case '\0': /* fin symbol */
169 /* we need at least a = since the last & */
171 /* terminate the value */
172 qrystr
->ptr
[i
] = '\0';
174 if (NULL
== (ds
= (data_string
*)array_get_unused_element(get_params
, TYPE_STRING
))) {
175 ds
= data_string_init();
177 buffer_copy_string_len(ds
->key
, key
, strlen(key
));
178 buffer_copy_string_len(ds
->value
, val
, strlen(val
));
180 array_insert_unique(get_params
, (data_unset
*)ds
);
183 key
= qrystr
->ptr
+ i
+ 1;
193 URIHANDLER_FUNC(mod_flv_streaming_path_handler
) {
194 plugin_data
*p
= p_d
;
200 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
202 if (buffer_string_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
204 mod_flv_streaming_patch_connection(srv
, con
, p
);
206 s_len
= buffer_string_length(con
->physical
.path
);
208 for (k
= 0; k
< p
->conf
.extensions
->used
; k
++) {
209 data_string
*ds
= (data_string
*)p
->conf
.extensions
->data
[k
];
210 int ct_len
= buffer_string_length(ds
->value
);
212 if (ct_len
> s_len
) continue;
213 if (buffer_is_empty(ds
->value
)) continue;
215 if (0 == strncmp(con
->physical
.path
->ptr
+ s_len
- ct_len
, ds
->value
->ptr
, ct_len
)) {
216 data_string
*get_param
;
217 off_t start
= 0, len
= -1;
219 /* if there is a start=[0-9]+ in the header use it as start,
220 * otherwise set start to beginning of file */
221 /* if there is a end=[0-9]+ in the header use it as end pos,
222 * otherwise send rest of file, starting from start */
224 array_reset(p
->get_params
);
225 buffer_copy_buffer(p
->query_str
, con
->uri
.query
);
226 split_get_params(p
->get_params
, p
->query_str
);
228 if (NULL
!= (get_param
= (data_string
*)array_get_element(p
->get_params
, "start"))) {
229 if (buffer_string_is_empty(get_param
->value
)) return HANDLER_GO_ON
;
230 start
= strtoll(get_param
->value
->ptr
, &err
, 10);
231 if (*err
!= '\0') return HANDLER_GO_ON
;
232 if (start
< 0) return HANDLER_GO_ON
;
235 if (NULL
!= (get_param
= (data_string
*)array_get_element(p
->get_params
, "end"))) {
237 if (buffer_string_is_empty(get_param
->value
)) return HANDLER_GO_ON
;
238 end
= strtoll(get_param
->value
->ptr
, &err
, 10);
239 if (*err
!= '\0') return HANDLER_GO_ON
;
240 if (end
< 0) return HANDLER_GO_ON
;
241 len
= (start
< end
? end
- start
: start
- end
) + 1;
243 else if (0 == start
) {
244 return HANDLER_GO_ON
;
247 /* let's build a flv header */
248 http_chunk_append_mem(srv
, con
, CONST_STR_LEN("FLV\x1\x1\0\0\0\x9\0\0\0\x9"));
249 if (0 != http_chunk_append_file_range(srv
, con
, con
->physical
.path
, start
, len
)) {
250 chunkqueue_reset(con
->write_queue
);
251 return HANDLER_GO_ON
;
254 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("video/x-flv"));
256 con
->file_finished
= 1;
258 return HANDLER_FINISHED
;
263 return HANDLER_GO_ON
;
266 /* this function is called at dlopen() time and inits the callbacks */
268 int mod_flv_streaming_plugin_init(plugin
*p
);
269 int mod_flv_streaming_plugin_init(plugin
*p
) {
270 p
->version
= LIGHTTPD_VERSION_ID
;
271 p
->name
= buffer_init_string("flv_streaming");
273 p
->init
= mod_flv_streaming_init
;
274 p
->handle_physical
= mod_flv_streaming_path_handler
;
275 p
->set_defaults
= mod_flv_streaming_set_defaults
;
276 p
->cleanup
= mod_flv_streaming_free
;