[mod_proxy] move data_fastcgi into mod_proxy.c
[lighttpd.git] / src / mod_flv_streaming.c
blobb0a3dbcdeefafafda46cc858c63c0cf563cd552f
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
6 #include "response.h"
7 #include "http_chunk.h"
9 #include "plugin.h"
11 #include <stdlib.h>
12 #include <string.h>
14 /* plugin config for all request/connections */
16 typedef struct {
17 array *extensions;
18 } plugin_config;
20 typedef struct {
21 PLUGIN_DATA;
23 buffer *query_str;
24 array *get_params;
26 plugin_config **config_storage;
28 plugin_config conf;
29 } plugin_data;
31 /* init the plugin data */
32 INIT_FUNC(mod_flv_streaming_init) {
33 plugin_data *p;
35 p = calloc(1, sizeof(*p));
37 p->query_str = buffer_init();
38 p->get_params = array_init();
40 return p;
43 /* detroy the plugin data */
44 FREE_FUNC(mod_flv_streaming_free) {
45 plugin_data *p = p_d;
47 UNUSED(srv);
49 if (!p) return HANDLER_GO_ON;
51 if (p->config_storage) {
52 size_t i;
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);
61 free(s);
63 free(p->config_storage);
66 buffer_free(p->query_str);
67 array_free(p->get_params);
69 free(p);
71 return HANDLER_GO_ON;
74 /* handle plugin config and check values */
76 SETDEFAULTS_FUNC(mod_flv_streaming_set_defaults) {
77 plugin_data *p = p_d;
78 size_t i = 0;
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];
91 plugin_config *s;
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;
114 #define PATCH(x) \
115 p->conf.x = s->x;
116 static int mod_flv_streaming_patch_connection(server *srv, connection *con, plugin_data *p) {
117 size_t i, j;
118 plugin_config *s = p->config_storage[0];
120 PATCH(extensions);
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;
130 /* merge config */
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"))) {
135 PATCH(extensions);
140 return 0;
142 #undef PATCH
144 static int split_get_params(array *get_params, buffer *qrystr) {
145 size_t is_key = 1;
146 size_t i, len;
147 char *key = NULL, *val = NULL;
149 key = qrystr->ptr;
151 /* we need the \0 */
152 len = buffer_string_length(qrystr);
153 for (i = 0; i <= len; i++) {
154 switch(qrystr->ptr[i]) {
155 case '=':
156 if (is_key) {
157 val = qrystr->ptr + i + 1;
159 qrystr->ptr[i] = '\0';
161 is_key = 0;
164 break;
165 case '&':
166 case '\0': /* fin symbol */
167 if (!is_key) {
168 data_string *ds;
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;
184 val = NULL;
185 is_key = 1;
186 break;
190 return 0;
193 URIHANDLER_FUNC(mod_flv_streaming_path_handler) {
194 plugin_data *p = p_d;
195 int s_len;
196 size_t k;
198 UNUSED(srv);
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;
218 char *err = NULL;
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"))) {
236 off_t 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;
262 /* not found */
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;
278 p->data = NULL;
280 return 0;