2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
#include "base.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "http_chunk.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2006-03-07 12:26:10 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-03-07 12:26:10 +00:00
|
|
|
|
|
|
|
/* plugin config for all request/connections */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
array *extensions;
|
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config **config_storage;
|
2006-10-04 13:26:23 +00:00
|
|
|
plugin_config conf;
|
2006-03-07 12:26:10 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
/* init the plugin data */
|
|
|
|
INIT_FUNC(mod_flv_streaming_init) {
|
2018-09-17 05:46:37 +00:00
|
|
|
return calloc(1, sizeof(plugin_data));
|
2006-03-07 12:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* detroy the plugin data */
|
|
|
|
FREE_FUNC(mod_flv_streaming_free) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
if (!p) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
if (p->config_storage) {
|
2018-09-17 05:46:37 +00:00
|
|
|
for (size_t i = 0; i < srv->config_context->used; ++i) {
|
2006-03-07 12:26:10 +00:00
|
|
|
plugin_config *s = p->config_storage[i];
|
2015-05-14 09:38:33 +00:00
|
|
|
if (NULL == s) continue;
|
2006-03-07 12:26:10 +00:00
|
|
|
array_free(s->extensions);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
free(p->config_storage);
|
|
|
|
}
|
|
|
|
free(p);
|
2018-09-17 05:46:37 +00:00
|
|
|
UNUSED(srv);
|
2006-03-07 12:26:10 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle plugin config and check values */
|
|
|
|
|
|
|
|
SETDEFAULTS_FUNC(mod_flv_streaming_set_defaults) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
size_t i = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
config_values_t cv[] = {
|
2006-03-07 12:26:10 +00:00
|
|
|
{ "flv-streaming.extensions", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
|
|
|
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
if (!p) return HANDLER_ERROR;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-02-27 12:42:48 +00:00
|
|
|
p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
2015-11-07 12:51:11 +00:00
|
|
|
data_config const* config = (data_config const*)srv->config_context->data[i];
|
2006-03-07 12:26:10 +00:00
|
|
|
plugin_config *s;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
s = calloc(1, sizeof(plugin_config));
|
|
|
|
s->extensions = array_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
cv[0].destination = s->extensions;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
p->config_storage[i] = s;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-11-07 12:51:11 +00:00
|
|
|
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
|
2006-03-07 12:26:10 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2017-03-05 20:39:45 +00:00
|
|
|
|
|
|
|
if (!array_is_vlist(s->extensions)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
|
|
|
"unexpected value for flv-streaming.extensions; expected list of \"ext\"");
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-03-07 12:26:10 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PATCH(x) \
|
|
|
|
p->conf.x = s->x;
|
|
|
|
static int mod_flv_streaming_patch_connection(server *srv, connection *con, plugin_data *p) {
|
|
|
|
size_t i, j;
|
|
|
|
plugin_config *s = p->config_storage[0];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
PATCH(extensions);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
/* skip the first, the global context */
|
|
|
|
for (i = 1; i < srv->config_context->used; i++) {
|
2019-10-17 05:27:52 +00:00
|
|
|
if (!config_check_cond(con, i)) continue; /* condition not matched */
|
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
data_config *dc = (data_config *)srv->config_context->data[i];
|
|
|
|
s = p->config_storage[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
/* merge config */
|
|
|
|
for (j = 0; j < dc->value->used; j++) {
|
|
|
|
data_unset *du = dc->value->data[j];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-13 07:40:03 +00:00
|
|
|
if (buffer_is_equal_string(&du->key, CONST_STR_LEN("flv-streaming.extensions"))) {
|
2006-03-07 12:26:10 +00:00
|
|
|
PATCH(extensions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#undef PATCH
|
|
|
|
|
2019-06-15 05:16:22 +00:00
|
|
|
static off_t get_param_value(buffer *qb, const char *m, size_t mlen) {
|
|
|
|
const char * const q = qb->ptr;
|
|
|
|
size_t len = buffer_string_length(qb);
|
|
|
|
if (len < mlen+2) return -1;
|
|
|
|
len -= (mlen+2);
|
|
|
|
for (size_t i = 0; i <= len; ++i) {
|
|
|
|
if (0 == memcmp(q+i, m, mlen) && q[i+mlen] == '=') {
|
|
|
|
char *err;
|
|
|
|
off_t n = strtoll(q+i+mlen+1, &err, 10);
|
|
|
|
return (*err == '\0' || *err == '&') ? n : -1;
|
|
|
|
}
|
|
|
|
do { ++i; } while (i < len && q[i] != '&');
|
|
|
|
}
|
|
|
|
return -1;
|
2006-03-07 12:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
URIHANDLER_FUNC(mod_flv_streaming_path_handler) {
|
|
|
|
plugin_data *p = p_d;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-08-01 16:13:34 +00:00
|
|
|
if (con->mode != DIRECT) return HANDLER_GO_ON;
|
2015-02-08 12:37:10 +00:00
|
|
|
if (buffer_string_is_empty(con->physical.path)) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
mod_flv_streaming_patch_connection(srv, con, p);
|
|
|
|
|
2018-09-17 05:46:37 +00:00
|
|
|
if (!array_match_value_suffix(p->conf.extensions, con->physical.path)) {
|
|
|
|
/* not found */
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2019-06-15 05:16:22 +00:00
|
|
|
off_t start = get_param_value(con->uri.query, CONST_STR_LEN("start"));
|
|
|
|
off_t end = get_param_value(con->uri.query, CONST_STR_LEN("end"));
|
|
|
|
off_t len = -1;
|
|
|
|
if (start < 0) start = 0;
|
|
|
|
if (start < end)
|
|
|
|
len = end - start + 1;
|
|
|
|
else if (0 == start)
|
|
|
|
return HANDLER_GO_ON; /* let mod_staticfile send whole file */
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
/* if there is a start=[0-9]+ in the header use it as start,
|
2017-01-20 16:21:46 +00:00
|
|
|
* otherwise set start to beginning of file */
|
|
|
|
/* if there is a end=[0-9]+ in the header use it as end pos,
|
|
|
|
* otherwise send rest of file, starting from start */
|
2006-03-07 12:26:10 +00:00
|
|
|
|
2016-03-30 10:39:33 +00:00
|
|
|
/* let's build a flv header */
|
|
|
|
http_chunk_append_mem(srv, con, CONST_STR_LEN("FLV\x1\x1\0\0\0\x9\0\0\0\x9"));
|
2017-01-20 16:21:46 +00:00
|
|
|
if (0 != http_chunk_append_file_range(srv, con, con->physical.path, start, len)) {
|
2016-03-30 10:39:33 +00:00
|
|
|
chunkqueue_reset(con->write_queue);
|
2006-03-07 12:26:10 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2018-09-09 05:50:33 +00:00
|
|
|
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("video/x-flv"));
|
2006-03-07 12:26:10 +00:00
|
|
|
con->file_finished = 1;
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this function is called at dlopen() time and inits the callbacks */
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
int mod_flv_streaming_plugin_init(plugin *p);
|
2006-03-07 12:26:10 +00:00
|
|
|
int mod_flv_streaming_plugin_init(plugin *p) {
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
2019-10-19 04:30:54 +00:00
|
|
|
p->name = "flv_streaming";
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
p->init = mod_flv_streaming_init;
|
|
|
|
p->handle_physical = mod_flv_streaming_path_handler;
|
|
|
|
p->set_defaults = mod_flv_streaming_set_defaults;
|
|
|
|
p->cleanup = mod_flv_streaming_free;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|