2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
#include "base.h"
|
2019-10-26 05:32:25 +00:00
|
|
|
#include "array.h"
|
2006-03-07 12:26:10 +00:00
|
|
|
#include "buffer.h"
|
2019-10-26 05:32:25 +00:00
|
|
|
#include "log.h"
|
2006-03-07 12:26:10 +00:00
|
|
|
#include "http_chunk.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2020-11-16 13:12:58 +00:00
|
|
|
#include "stat_cache.h"
|
2006-03-07 12:26:10 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2020-11-16 13:12:58 +00:00
|
|
|
#include <errno.h>
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-03-07 12:26:10 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2019-10-26 05:32:25 +00:00
|
|
|
const array *extensions;
|
2006-03-07 12:26:10 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-10-26 05:32:25 +00:00
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config defaults;
|
|
|
|
plugin_config conf;
|
2006-03-07 12:26:10 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
INIT_FUNC(mod_flv_streaming_init) {
|
2019-10-26 05:32:25 +00:00
|
|
|
return calloc(1, sizeof(plugin_data));
|
2006-03-07 12:26:10 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 05:32:25 +00:00
|
|
|
static void mod_flv_streaming_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
|
|
|
|
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
|
|
|
|
case 0: /* flv-streaming.extensions */
|
|
|
|
pconf->extensions = cpv->v.a;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 05:32:25 +00:00
|
|
|
static void mod_flv_streaming_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_flv_streaming_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
|
|
|
}
|
2019-10-17 05:27:52 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_flv_streaming_patch_config(request_st * const r, plugin_data * const p) {
|
2020-01-11 16:07:43 +00:00
|
|
|
p->conf = p->defaults; /* copy small struct instead of memcpy() */
|
|
|
|
/*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
|
2019-10-26 05:32:25 +00:00
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
2020-01-13 02:51:12 +00:00
|
|
|
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
|
2019-10-26 05:32:25 +00:00
|
|
|
mod_flv_streaming_merge_config(&p->conf,
|
|
|
|
p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 05:32:25 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_flv_streaming_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("flv-streaming.extensions"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_VLIST,
|
2019-10-26 05:32:25 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ NULL, 0,
|
|
|
|
T_CONFIG_UNSET,
|
|
|
|
T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
|
|
|
|
|
|
|
plugin_data * const p = p_d;
|
|
|
|
if (!config_plugin_values_init(srv, p, cpk, "mod_flv_streaming"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
|
|
|
/* initialize p->defaults from global config context */
|
|
|
|
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
|
|
|
|
const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
|
|
|
|
if (-1 != cpv->k_id)
|
|
|
|
mod_flv_streaming_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 05:32:25 +00:00
|
|
|
return HANDLER_GO_ON;
|
2006-03-07 12:26:10 +00:00
|
|
|
}
|
|
|
|
|
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;
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
size_t len = buffer_clen(qb);
|
2019-06-15 05:16:22 +00:00
|
|
|
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
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (NULL != r->handler_module) return HANDLER_GO_ON;
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_blank(&r->physical.path)) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_flv_streaming_patch_config(r, p);
|
2019-10-26 05:32:25 +00:00
|
|
|
if (NULL == p->conf.extensions) return HANDLER_GO_ON;
|
2006-03-07 12:26:10 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (!array_match_value_suffix(p->conf.extensions, &r->physical.path)) {
|
2018-09-17 05:46:37 +00:00
|
|
|
/* not found */
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
off_t start = get_param_value(&r->uri.query, CONST_STR_LEN("start"));
|
|
|
|
off_t end = get_param_value(&r->uri.query, CONST_STR_LEN("end"));
|
2019-06-15 05:16:22 +00:00
|
|
|
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 */
|
|
|
|
|
2020-11-16 13:12:58 +00:00
|
|
|
stat_cache_entry * const sce =
|
|
|
|
stat_cache_get_entry_open(&r->physical.path, r->conf.follow_symlink);
|
|
|
|
if (NULL == sce || (sce->fd < 0 && 0 != sce->st.st_size)) {
|
|
|
|
r->http_status = (errno == ENOENT) ? 404 : 403;
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
2021-05-14 06:29:24 +00:00
|
|
|
if (len == -1 || sce->st.st_size - start < len)
|
|
|
|
len = sce->st.st_size - start;
|
|
|
|
if (len <= 0)
|
2020-11-16 13:12:58 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
|
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 */
|
2020-01-13 02:51:12 +00:00
|
|
|
http_chunk_append_mem(r, CONST_STR_LEN("FLV\x1\x1\0\0\0\x9\0\0\0\x9"));
|
2020-11-16 13:12:58 +00:00
|
|
|
http_chunk_append_file_ref_range(r, sce, start, len);
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("video/x-flv"));
|
|
|
|
r->resp_body_finished = 1;
|
2006-03-07 12:26:10 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-07 12:26:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|