7 #include "http_chunk.h"
8 #include "stat_cache.h"
16 /* plugin config for all request/connections */
28 plugin_config
**config_storage
;
33 /* init the plugin data */
34 INIT_FUNC(mod_flv_streaming_init
) {
37 p
= calloc(1, sizeof(*p
));
39 p
->query_str
= buffer_init();
40 p
->get_params
= array_init();
45 /* detroy the plugin data */
46 FREE_FUNC(mod_flv_streaming_free
) {
51 if (!p
) return HANDLER_GO_ON
;
53 if (p
->config_storage
) {
56 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
57 plugin_config
*s
= p
->config_storage
[i
];
59 if (NULL
== s
) continue;
61 array_free(s
->extensions
);
65 free(p
->config_storage
);
68 buffer_free(p
->query_str
);
69 array_free(p
->get_params
);
76 /* handle plugin config and check values */
78 SETDEFAULTS_FUNC(mod_flv_streaming_set_defaults
) {
82 config_values_t cv
[] = {
83 { "flv-streaming.extensions", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
84 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
87 if (!p
) return HANDLER_ERROR
;
89 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
91 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
92 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
95 s
= calloc(1, sizeof(plugin_config
));
96 s
->extensions
= array_init();
98 cv
[0].destination
= s
->extensions
;
100 p
->config_storage
[i
] = s
;
102 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
103 return HANDLER_ERROR
;
107 return HANDLER_GO_ON
;
112 static int mod_flv_streaming_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
114 plugin_config
*s
= p
->config_storage
[0];
118 /* skip the first, the global context */
119 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
120 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
121 s
= p
->config_storage
[i
];
123 /* condition didn't match */
124 if (!config_check_cond(srv
, con
, dc
)) continue;
127 for (j
= 0; j
< dc
->value
->used
; j
++) {
128 data_unset
*du
= dc
->value
->data
[j
];
130 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("flv-streaming.extensions"))) {
140 static int split_get_params(array
*get_params
, buffer
*qrystr
) {
143 char *key
= NULL
, *val
= NULL
;
148 len
= buffer_string_length(qrystr
);
149 for (i
= 0; i
<= len
; i
++) {
150 switch(qrystr
->ptr
[i
]) {
153 val
= qrystr
->ptr
+ i
+ 1;
155 qrystr
->ptr
[i
] = '\0';
162 case '\0': /* fin symbol */
165 /* we need at least a = since the last & */
167 /* terminate the value */
168 qrystr
->ptr
[i
] = '\0';
170 if (NULL
== (ds
= (data_string
*)array_get_unused_element(get_params
, TYPE_STRING
))) {
171 ds
= data_string_init();
173 buffer_copy_string_len(ds
->key
, key
, strlen(key
));
174 buffer_copy_string_len(ds
->value
, val
, strlen(val
));
176 array_insert_unique(get_params
, (data_unset
*)ds
);
179 key
= qrystr
->ptr
+ i
+ 1;
189 URIHANDLER_FUNC(mod_flv_streaming_path_handler
) {
190 plugin_data
*p
= p_d
;
196 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
198 if (buffer_string_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
200 mod_flv_streaming_patch_connection(srv
, con
, p
);
202 s_len
= buffer_string_length(con
->physical
.path
);
204 for (k
= 0; k
< p
->conf
.extensions
->used
; k
++) {
205 data_string
*ds
= (data_string
*)p
->conf
.extensions
->data
[k
];
206 int ct_len
= buffer_string_length(ds
->value
);
208 if (ct_len
> s_len
) continue;
209 if (buffer_is_empty(ds
->value
)) continue;
211 if (0 == strncmp(con
->physical
.path
->ptr
+ s_len
- ct_len
, ds
->value
->ptr
, ct_len
)) {
212 data_string
*get_param
;
215 /* if there is a start=[0-9]+ in the header use it as start,
216 * otherwise send the full file */
218 array_reset(p
->get_params
);
219 buffer_copy_buffer(p
->query_str
, con
->uri
.query
);
220 split_get_params(p
->get_params
, p
->query_str
);
222 if (NULL
== (get_param
= (data_string
*)array_get_element(p
->get_params
, "start"))) {
223 return HANDLER_GO_ON
;
227 if (buffer_string_is_empty(get_param
->value
)) return HANDLER_GO_ON
;
229 /* check if it is a number */
230 start
= strtol(get_param
->value
->ptr
, &err
, 10);
232 return HANDLER_GO_ON
;
235 if (start
<= 0) return HANDLER_GO_ON
;
237 /* let's build a flv header */
238 http_chunk_append_mem(srv
, con
, CONST_STR_LEN("FLV\x1\x1\0\0\0\x9\0\0\0\x9"));
239 if (0 != http_chunk_append_file_range(srv
, con
, con
->physical
.path
, start
, -1)) {
240 chunkqueue_reset(con
->write_queue
);
241 return HANDLER_GO_ON
;
244 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("video/x-flv"));
246 con
->file_finished
= 1;
248 return HANDLER_FINISHED
;
253 return HANDLER_GO_ON
;
256 /* this function is called at dlopen() time and inits the callbacks */
258 int mod_flv_streaming_plugin_init(plugin
*p
);
259 int mod_flv_streaming_plugin_init(plugin
*p
) {
260 p
->version
= LIGHTTPD_VERSION_ID
;
261 p
->name
= buffer_init_string("flv_streaming");
263 p
->init
= mod_flv_streaming_init
;
264 p
->handle_physical
= mod_flv_streaming_path_handler
;
265 p
->set_defaults
= mod_flv_streaming_set_defaults
;
266 p
->cleanup
= mod_flv_streaming_free
;