xref: /lighttpd1.4/src/mod_staticfile.c (revision 5e14db43)
18abd06a7SGlenn Strauss #include "first.h"
28abd06a7SGlenn Strauss 
3d8394f7fSJan Kneschke #include "log.h"
4d8394f7fSJan Kneschke #include "buffer.h"
5d8394f7fSJan Kneschke 
6d8394f7fSJan Kneschke #include "plugin.h"
7d8394f7fSJan Kneschke 
867c0b149SGlenn Strauss #include "request.h"
9d8394f7fSJan Kneschke #include "response.h"
109a5e1652SGlenn Strauss #include "stat_cache.h"
11d8394f7fSJan Kneschke 
1222e8b456SStefan Bühler #include <stdlib.h>
1322e8b456SStefan Bühler #include <string.h>
1422e8b456SStefan Bühler 
15d8394f7fSJan Kneschke /**
16d8394f7fSJan Kneschke  * this is a staticfile for a lighttpd plugin
17d8394f7fSJan Kneschke  *
18d8394f7fSJan Kneschke  */
19d8394f7fSJan Kneschke 
20d8394f7fSJan Kneschke 
21d8394f7fSJan Kneschke typedef struct {
2249140fe3SGlenn Strauss 	const array *exclude_ext;
23b2a96c95SJan Kneschke 	unsigned short etags_used;
24e05f1b3eSStefan Bühler 	unsigned short disable_pathinfo;
25d8394f7fSJan Kneschke } plugin_config;
26d8394f7fSJan Kneschke 
27d8394f7fSJan Kneschke typedef struct {
28d8394f7fSJan Kneschke     PLUGIN_DATA;
2949140fe3SGlenn Strauss     plugin_config defaults;
30d8394f7fSJan Kneschke     plugin_config conf;
31d8394f7fSJan Kneschke } plugin_data;
32d8394f7fSJan Kneschke 
INIT_FUNC(mod_staticfile_init)33d8394f7fSJan Kneschke INIT_FUNC(mod_staticfile_init) {
34*5e14db43SGlenn Strauss     return ck_calloc(1, sizeof(plugin_data));
35d8394f7fSJan Kneschke }
36d8394f7fSJan Kneschke 
mod_staticfile_merge_config_cpv(plugin_config * const pconf,const config_plugin_value_t * const cpv)3749140fe3SGlenn Strauss static void mod_staticfile_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
3849140fe3SGlenn Strauss     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
3949140fe3SGlenn Strauss       case 0: /* static-file.exclude-extensions */
4049140fe3SGlenn Strauss         pconf->exclude_ext = cpv->v.a;
4149140fe3SGlenn Strauss         break;
4249140fe3SGlenn Strauss       case 1: /* static-file.etags */
4349140fe3SGlenn Strauss         pconf->etags_used = cpv->v.u;
4449140fe3SGlenn Strauss         break;
4549140fe3SGlenn Strauss       case 2: /* static-file.disable-pathinfo */
4649140fe3SGlenn Strauss         pconf->disable_pathinfo = cpv->v.u;
4749140fe3SGlenn Strauss         break;
4849140fe3SGlenn Strauss       default:/* should not happen */
4949140fe3SGlenn Strauss         return;
5049140fe3SGlenn Strauss     }
5149140fe3SGlenn Strauss }
5249140fe3SGlenn Strauss 
mod_staticfile_merge_config(plugin_config * const pconf,const config_plugin_value_t * cpv)5349140fe3SGlenn Strauss static void mod_staticfile_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
5449140fe3SGlenn Strauss     do {
5549140fe3SGlenn Strauss         mod_staticfile_merge_config_cpv(pconf, cpv);
5649140fe3SGlenn Strauss     } while ((++cpv)->k_id != -1);
5749140fe3SGlenn Strauss }
5849140fe3SGlenn Strauss 
mod_staticfile_patch_config(request_st * const r,plugin_data * const p)597c7f8c46SGlenn Strauss static void mod_staticfile_patch_config(request_st * const r, plugin_data * const p) {
60cc2134c8SGlenn Strauss     p->conf = p->defaults; /* copy small struct instead of memcpy() */
61cc2134c8SGlenn Strauss     /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
6249140fe3SGlenn Strauss     for (int i = 1, used = p->nconfig; i < used; ++i) {
637c7f8c46SGlenn Strauss         if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
6449140fe3SGlenn Strauss             mod_staticfile_merge_config(&p->conf,
6549140fe3SGlenn Strauss                                         p->cvlist + p->cvlist[i].v.u2[0]);
6649140fe3SGlenn Strauss     }
6749140fe3SGlenn Strauss }
68d8394f7fSJan Kneschke 
SETDEFAULTS_FUNC(mod_staticfile_set_defaults)69d8394f7fSJan Kneschke SETDEFAULTS_FUNC(mod_staticfile_set_defaults) {
7049140fe3SGlenn Strauss     static const config_plugin_keys_t cpk[] = {
7149140fe3SGlenn Strauss       { CONST_STR_LEN("static-file.exclude-extensions"),
7203b4c993SGlenn Strauss         T_CONFIG_ARRAY_VLIST,
7349140fe3SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
7449140fe3SGlenn Strauss      ,{ CONST_STR_LEN("static-file.etags"),
7549140fe3SGlenn Strauss         T_CONFIG_BOOL,
7649140fe3SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
7749140fe3SGlenn Strauss      ,{ CONST_STR_LEN("static-file.disable-pathinfo"),
7849140fe3SGlenn Strauss         T_CONFIG_BOOL,
7949140fe3SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
8049140fe3SGlenn Strauss      ,{ NULL, 0,
8149140fe3SGlenn Strauss         T_CONFIG_UNSET,
8249140fe3SGlenn Strauss         T_CONFIG_SCOPE_UNSET }
83d8394f7fSJan Kneschke     };
84d8394f7fSJan Kneschke 
8549140fe3SGlenn Strauss     plugin_data * const p = p_d;
8649140fe3SGlenn Strauss     if (!config_plugin_values_init(srv, p, cpk, "mod_staticfile"))
8749140fe3SGlenn Strauss         return HANDLER_ERROR;
88d8394f7fSJan Kneschke 
8949140fe3SGlenn Strauss     /* initialize p->defaults from global config context */
9049140fe3SGlenn Strauss     p->defaults.etags_used = 1; /* etags enabled */
9149140fe3SGlenn Strauss     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
9249140fe3SGlenn Strauss         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
9349140fe3SGlenn Strauss         if (-1 != cpv->k_id)
9449140fe3SGlenn Strauss             mod_staticfile_merge_config(&p->defaults, cpv);
9549140fe3SGlenn Strauss     }
96d8394f7fSJan Kneschke 
97d8394f7fSJan Kneschke     return HANDLER_GO_ON;
98d8394f7fSJan Kneschke }
99d8394f7fSJan Kneschke 
10057c8b328SGlenn Strauss __attribute_cold__
10157c8b328SGlenn Strauss static handler_t
mod_staticfile_not_handled(request_st * const r,const char * const msg)10257c8b328SGlenn Strauss mod_staticfile_not_handled(request_st * const r, const char * const msg)
10357c8b328SGlenn Strauss {
10457c8b328SGlenn Strauss     if (r->conf.log_request_handling)
10557c8b328SGlenn Strauss         log_error(r->conf.errh, __FILE__, __LINE__,
10657c8b328SGlenn Strauss           "-- NOT handling file as static file, %s forbidden", msg);
10757c8b328SGlenn Strauss     return HANDLER_GO_ON;
10857c8b328SGlenn Strauss }
109d8394f7fSJan Kneschke 
11091472ab7SGlenn Strauss static handler_t
mod_staticfile_process(request_st * const r,plugin_config * const pconf)11191472ab7SGlenn Strauss mod_staticfile_process (request_st * const r, plugin_config * const pconf)
11291472ab7SGlenn Strauss {
11391472ab7SGlenn Strauss     if (pconf->disable_pathinfo && !buffer_is_blank(&r->pathinfo)) {
11457c8b328SGlenn Strauss         return mod_staticfile_not_handled(r, "pathinfo");
115e05f1b3eSStefan Bühler     }
116e05f1b3eSStefan Bühler 
11791472ab7SGlenn Strauss     if (pconf->exclude_ext
11891472ab7SGlenn Strauss         && array_match_value_suffix(pconf->exclude_ext, &r->physical.path)) {
11957c8b328SGlenn Strauss         return mod_staticfile_not_handled(r, "extension");
120d8394f7fSJan Kneschke     }
121d8394f7fSJan Kneschke 
12291472ab7SGlenn Strauss     if (!pconf->etags_used) r->conf.etag_flags = 0;
1239a5e1652SGlenn Strauss 
1249a5e1652SGlenn Strauss     /* r->tmp_sce is set in http_response_physical_path_check() and is valid
1259a5e1652SGlenn Strauss      * in handle_subrequest_start callback -- handle_subrequest_start callbacks
1269a5e1652SGlenn Strauss      * should not change r->physical.path (or should invalidate r->tmp_sce) */
1279a5e1652SGlenn Strauss     if (r->tmp_sce && !buffer_is_equal(&r->tmp_sce->name, &r->physical.path))
1289a5e1652SGlenn Strauss         r->tmp_sce = NULL;
1299a5e1652SGlenn Strauss 
1309a5e1652SGlenn Strauss     http_response_send_file(r, &r->physical.path, r->tmp_sce);
131d8394f7fSJan Kneschke 
132d8394f7fSJan Kneschke     return HANDLER_FINISHED;
133d8394f7fSJan Kneschke }
134d8394f7fSJan Kneschke 
URIHANDLER_FUNC(mod_staticfile_subrequest)13591472ab7SGlenn Strauss URIHANDLER_FUNC(mod_staticfile_subrequest) {
13691472ab7SGlenn Strauss     if (NULL != r->handler_module) return HANDLER_GO_ON;
137d0494fc0SGlenn Strauss     if (!http_method_get_head_query_post(r->http_method)) return HANDLER_GO_ON;
13891472ab7SGlenn Strauss     /* r->physical.path is non-empty for handle_subrequest_start */
13991472ab7SGlenn Strauss     /*if (buffer_is_blank(&r->physical.path)) return HANDLER_GO_ON;*/
14091472ab7SGlenn Strauss 
14191472ab7SGlenn Strauss     plugin_data * const p = p_d;
14291472ab7SGlenn Strauss     mod_staticfile_patch_config(r, p);
14391472ab7SGlenn Strauss 
14491472ab7SGlenn Strauss     return mod_staticfile_process(r, &p->conf);
14591472ab7SGlenn Strauss }
14691472ab7SGlenn Strauss 
147d8394f7fSJan Kneschke 
148b82d7b8aSGlenn Strauss __attribute_cold__
14963f785a2SStefan Bühler int mod_staticfile_plugin_init(plugin *p);
mod_staticfile_plugin_init(plugin * p)150d8394f7fSJan Kneschke int mod_staticfile_plugin_init(plugin *p) {
151d8394f7fSJan Kneschke 	p->version     = LIGHTTPD_VERSION_ID;
152e2de4e58SGlenn Strauss 	p->name        = "staticfile";
153d8394f7fSJan Kneschke 
154d8394f7fSJan Kneschke 	p->init        = mod_staticfile_init;
155d8394f7fSJan Kneschke 	p->handle_subrequest_start = mod_staticfile_subrequest;
156d8394f7fSJan Kneschke 	p->set_defaults  = mod_staticfile_set_defaults;
157d8394f7fSJan Kneschke 
158d8394f7fSJan Kneschke 	return 0;
159d8394f7fSJan Kneschke }
160