|
#include <ctype.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "base.h"
|
|
#include "log.h"
|
|
#include "buffer.h"
|
|
|
|
#include "plugin.h"
|
|
#include <unistd.h>
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "config.h"
|
|
#endif
|
|
|
|
/**
|
|
* small lighttpd-plugin for selective speed
|
|
*
|
|
* Bjoern Kalkbrenner <terminar@cyberphoria.org> [20070204]
|
|
*
|
|
* Adds: X-LIGHTTPD-KBytes-per-second header, you now can use e.g.
|
|
* <?php
|
|
* header("X-LIGHTTPD-KBytes-per-second: 20");
|
|
* header("X-Sendfile: /path/to/file");
|
|
* ?>
|
|
* to set the max speed for the download/current connection. This can be also used with reproxy.
|
|
* Options:
|
|
* # use this on the frontend to just copy the header from the response to the request header (forward it to the backend)
|
|
* speed.just-copy-header = "enable"
|
|
* # use this on the backend to fetch the speed from the request. This is not useful on the frontend, i think :)
|
|
* speed.use-request = "enable"
|
|
*
|
|
*/
|
|
|
|
|
|
|
|
/* plugin config for all request/connections */
|
|
|
|
typedef struct {
|
|
unsigned short just_copy_header;
|
|
unsigned short use_request;
|
|
} plugin_config;
|
|
|
|
typedef struct {
|
|
PLUGIN_DATA;
|
|
|
|
buffer *tmp_buf;
|
|
|
|
plugin_config **config_storage;
|
|
|
|
plugin_config conf;
|
|
} plugin_data;
|
|
|
|
/* init the plugin data */
|
|
INIT_FUNC(mod_speed_init) {
|
|
plugin_data *p;
|
|
|
|
UNUSED(srv);
|
|
|
|
p = calloc(1, sizeof(*p));
|
|
|
|
return p;
|
|
}
|
|
|
|
/* detroy the plugin data */
|
|
FREE_FUNC(mod_speed_free) {
|
|
plugin_data *p = p_d;
|
|
|
|
UNUSED(srv);
|
|
|
|
if (!p) return HANDLER_GO_ON;
|
|
|
|
if (p->config_storage) {
|
|
size_t i;
|
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
|
plugin_config *s = p->config_storage[i];
|
|
|
|
if (!s) continue;
|
|
|
|
free(s);
|
|
}
|
|
free(p->config_storage);
|
|
}
|
|
|
|
free(p);
|
|
|
|
return HANDLER_GO_ON;
|
|
}
|
|
|
|
/* handle plugin config and check values */
|
|
SETDEFAULTS_FUNC(mod_speed_set_defaults) {
|
|
plugin_data *p = p_d;
|
|
size_t i = 0;
|
|
|
|
config_values_t cv[] = {
|
|
{ "speed.just-copy-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
|
|
{ "speed.use-request", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
|
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
|
};
|
|
|
|
if (!p) return HANDLER_ERROR;
|
|
|
|
p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
|
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
|
plugin_config *s;
|
|
|
|
s = calloc(1, sizeof(plugin_config));
|
|
s->just_copy_header = 0;
|
|
s->use_request = 0;
|
|
|
|
cv[0].destination = &(s->just_copy_header);
|
|
cv[1].destination = &(s->use_request);
|
|
|
|
p->config_storage[i] = s;
|
|
|
|
if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
|
|
TRACE("%s","error inserting values config");
|
|
return HANDLER_ERROR;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return HANDLER_GO_ON;
|
|
}
|
|
|
|
static int mod_speed_patch_connection(server *srv, connection *con, plugin_data *p) {
|
|
size_t i, j;
|
|
plugin_config *s = p->config_storage[0];
|
|
|
|
PATCH_OPTION(just_copy_header);
|
|
PATCH_OPTION(use_request);
|
|
|
|
/* skip the first, the global context */
|
|
for (i = 1; i < srv->config_context->used; i++) {
|
|
|
|
data_config *dc = (data_config *)srv->config_context->data[i];
|
|
s = p->config_storage[i];
|
|
|
|
/* condition didn't match */
|
|
if (!config_check_cond(srv, con, dc)) continue;
|
|
|
|
/* merge config */
|
|
for (j = 0; j < dc->value->used; j++) {
|
|
data_unset *du = dc->value->data[j];
|
|
|
|
if (buffer_is_equal_string(du->key, CONST_STR_LEN("speed.just-copy-header"))) {
|
|
TRACE("%s","copy header set");
|
|
PATCH_OPTION(just_copy_header);
|
|
}
|
|
|
|
if (buffer_is_equal_string(du->key, CONST_STR_LEN("speed.use-request"))) {
|
|
TRACE("%s","use request header set");
|
|
PATCH_OPTION(use_request);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
URIHANDLER_FUNC(mod_speed_send_request_content) {
|
|
plugin_data *p = p_d;
|
|
buffer *b=NULL;
|
|
data_string *ds = NULL;
|
|
data_string *ds_request = NULL;
|
|
int found=0;
|
|
|
|
mod_speed_patch_connection(srv, con, p);
|
|
|
|
if (NULL != (ds = (data_string *)array_get_element(con->response.headers, CONST_STR_LEN("X-LIGHTTPD-KBytes-per-second")))) {
|
|
if (p->conf.just_copy_header > 0) {
|
|
|
|
if (NULL == (ds_request = (data_string *)array_get_element(con->request.headers, CONST_STR_LEN("X-LIGHTTPD-KBytes-per-second")))) {
|
|
|
|
if (NULL == (ds_request = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
|
|
ds_request = data_string_init();
|
|
}
|
|
|
|
} else {
|
|
found=1;
|
|
}
|
|
|
|
buffer_copy_string(ds_request->key, "X-LIGHTTPD-KBytes-per-second");
|
|
buffer_copy_string(ds_request->value, ds->value->ptr);
|
|
if (!found)
|
|
{
|
|
array_insert_unique(con->request.headers, (data_unset *)ds_request);
|
|
}
|
|
|
|
} else
|
|
{
|
|
con->conf.kbytes_per_second = atol(ds->value->ptr);
|
|
TRACE("===> Found my header for speed in send_request_content: %s",ds->value->ptr);
|
|
}
|
|
|
|
}
|
|
|
|
/* not found */
|
|
return HANDLER_GO_ON;
|
|
}
|
|
|
|
URIHANDLER_FUNC(mod_speed_start_backend) {
|
|
plugin_data *p = p_d;
|
|
buffer *b=NULL;
|
|
data_string *ds = NULL;
|
|
data_string *ds_request = NULL;
|
|
|
|
mod_speed_patch_connection(srv, con, p);
|
|
|
|
if (p->conf.use_request > 0)
|
|
{
|
|
if (NULL != (ds = (data_string *)array_get_element(con->request.headers, CONST_STR_LEN("X-LIGHTTPD-KBytes-per-second")))) {
|
|
|
|
con->conf.kbytes_per_second = atol(ds->value->ptr);
|
|
}
|
|
}
|
|
|
|
/* not found */
|
|
return HANDLER_GO_ON;
|
|
}
|
|
|
|
|
|
/* this function is called at dlopen() time and inits the callbacks */
|
|
|
|
int mod_speed_plugin_init(plugin *p) {
|
|
p->version = LIGHTTPD_VERSION_ID;
|
|
p->name = buffer_init_string("speed");
|
|
|
|
p->init = mod_speed_init;
|
|
p->set_defaults = mod_speed_set_defaults;
|
|
p->cleanup = mod_speed_free;
|
|
|
|
p->handle_start_backend = mod_speed_start_backend;
|
|
p->handle_send_request_content = mod_speed_send_request_content;
|
|
|
|
p->data = NULL;
|
|
|
|
return 0;
|
|
}
|