You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lighttpd1.4/src/mod_flv_streaming.c

167 lines
5.1 KiB

#include "first.h"
#include "base.h"
#include "array.h"
#include "buffer.h"
#include "log.h"
#include "http_chunk.h"
#include "http_header.h"
#include "plugin.h"
#include <stdlib.h>
#include <string.h>
typedef struct {
const array *extensions;
} plugin_config;
typedef struct {
PLUGIN_DATA;
plugin_config defaults;
plugin_config conf;
} plugin_data;
INIT_FUNC(mod_flv_streaming_init) {
return calloc(1, sizeof(plugin_data));
}
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;
}
}
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);
}
static void mod_flv_streaming_patch_config(connection * const con, plugin_data * const p) {
memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
for (int i = 1, used = p->nconfig; i < used; ++i) {
if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id))
mod_flv_streaming_merge_config(&p->conf,
p->cvlist + p->cvlist[i].v.u2[0]);
}
}
SETDEFAULTS_FUNC(mod_flv_streaming_set_defaults) {
static const config_plugin_keys_t cpk[] = {
{ CONST_STR_LEN("flv-streaming.extensions"),
T_CONFIG_ARRAY,
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;
/* process and validate config directives
* (init i to 0 if global context; to 1 to skip empty global context) */
for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
for (; -1 != cpv->k_id; ++cpv) {
switch (cpv->k_id) {
case 0: /* flv-streaming.extensions */
if (!array_is_vlist(cpv->v.a)) {
log_error(srv->errh, __FILE__, __LINE__,
"unexpected value for %s; "
"expected list of \"ext\"", cpk[cpv->k_id].k);
return HANDLER_ERROR;
}
break;
default:/* should not happen */
break;
}
}
}
/* 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);
}
return HANDLER_GO_ON;
}
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;
}
URIHANDLER_FUNC(mod_flv_streaming_path_handler) {
plugin_data *p = p_d;
UNUSED(srv);
if (con->mode != DIRECT) return HANDLER_GO_ON;
fix buffer, chunk and http_chunk API * remove unused structs and functions (buffer_array, read_buffer) * change return type from int to void for many functions, as the return value (indicating error/success) was never checked, and the function would only fail on programming errors and not on invalid input; changed functions to use force_assert instead of returning an error. * all "len" parameters now are the real size of the memory to be read. the length of strings is given always without the terminating 0. * the "buffer" struct still counts the terminating 0 in ->used, provide buffer_string_length() to get the length of a string in a buffer. unset config "strings" have used == 0, which is used in some places to distinguish unset values from "" (empty string) values. * most buffer usages should now use it as string container. * optimise some buffer copying by "moving" data to other buffers * use (u)intmax_t for generic int-to-string functions * remove unused enum values: UNUSED_CHUNK, ENCODING_UNSET * converted BUFFER_APPEND_SLASH to inline function (no macro feature needed) * refactor: create chunkqueue_steal: moving (partial) chunks into another queue * http_chunk: added separate function to terminate chunked body instead of magic handling in http_chunk_append_mem(). http_chunk_append_* now handle empty chunks, and never terminate the chunked body. From: Stefan Bühler <stbuehler@web.de> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
8 years ago
if (buffer_string_is_empty(con->physical.path)) return HANDLER_GO_ON;
mod_flv_streaming_patch_config(con, p);
if (NULL == p->conf.extensions) return HANDLER_GO_ON;
if (!array_match_value_suffix(p->conf.extensions, con->physical.path)) {
/* not found */
return HANDLER_GO_ON;
}
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 */
/* if there is a start=[0-9]+ in the header use it as start,
* 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 */
[core] open fd when appending file to cq (fixes #2655) http_chunk_append_file() opens fd when appending file to chunkqueue. Defers calculation of content length until response is finished. This reduces race conditions pertaining to stat() and then (later) open(), when the result of the stat() was used for Content-Length or to generate chunked headers. Note: this does not change how lighttpd handles files that are modified in-place by another process after having been opened by lighttpd -- don't do that. This *does* improve handling of files that are frequently modified via a temporary file and then atomically renamed into place. mod_fastcgi has been modified to use http_chunk_append_file_range() with X-Sendfile2 and will open the target file multiple times if there are multiple ranges. Note: (future todo) not implemented for chunk.[ch] interfaces used by range requests in mod_staticfile or by mod_ssi. Those uses could lead to too many open fds. For mod_staticfile, limits should be put in place for max number of ranges accepted by mod_staticfile. For mod_ssi, limits would need to be placed on the maximum number of includes, and the primary SSI file split across lots of SSI directives should either copy the pieces or perhaps chunk.h could be extended to allow for an open fd to be shared across multiple chunks. Doing either of these would improve the performance of SSI since they would replace many file opens on the pieces of the SSI file around the SSI directives. x-ref: "Serving a file that is getting updated can cause an empty response or incorrect content-length error" https://redmine.lighttpd.net/issues/2655 github: Closes #49
7 years ago
/* let's build a flv header */
http_chunk_append_mem(con, CONST_STR_LEN("FLV\x1\x1\0\0\0\x9\0\0\0\x9"));
if (0 != http_chunk_append_file_range(con, con->physical.path, start, len)) {
[core] open fd when appending file to cq (fixes #2655) http_chunk_append_file() opens fd when appending file to chunkqueue. Defers calculation of content length until response is finished. This reduces race conditions pertaining to stat() and then (later) open(), when the result of the stat() was used for Content-Length or to generate chunked headers. Note: this does not change how lighttpd handles files that are modified in-place by another process after having been opened by lighttpd -- don't do that. This *does* improve handling of files that are frequently modified via a temporary file and then atomically renamed into place. mod_fastcgi has been modified to use http_chunk_append_file_range() with X-Sendfile2 and will open the target file multiple times if there are multiple ranges. Note: (future todo) not implemented for chunk.[ch] interfaces used by range requests in mod_staticfile or by mod_ssi. Those uses could lead to too many open fds. For mod_staticfile, limits should be put in place for max number of ranges accepted by mod_staticfile. For mod_ssi, limits would need to be placed on the maximum number of includes, and the primary SSI file split across lots of SSI directives should either copy the pieces or perhaps chunk.h could be extended to allow for an open fd to be shared across multiple chunks. Doing either of these would improve the performance of SSI since they would replace many file opens on the pieces of the SSI file around the SSI directives. x-ref: "Serving a file that is getting updated can cause an empty response or incorrect content-length error" https://redmine.lighttpd.net/issues/2655 github: Closes #49
7 years ago
chunkqueue_reset(con->write_queue);
return HANDLER_GO_ON;
}
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("video/x-flv"));
con->file_finished = 1;
return HANDLER_FINISHED;
}
int mod_flv_streaming_plugin_init(plugin *p);
int mod_flv_streaming_plugin_init(plugin *p) {
p->version = LIGHTTPD_VERSION_ID;
p->name = "flv_streaming";
p->init = mod_flv_streaming_init;
p->handle_physical = mod_flv_streaming_path_handler;
p->set_defaults = mod_flv_streaming_set_defaults;
return 0;
}