[mod_openssl] remove erroneous SSL_set_shutdown()
[lighttpd.git] / src / mod_staticfile.c
blobf72ee3fd16cfa156081097a58abcb63431c07ba2
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
7 #include "plugin.h"
9 #include "etag.h"
10 #include "http_chunk.h"
11 #include "response.h"
13 #include <stdlib.h>
14 #include <string.h>
16 /**
17 * this is a staticfile for a lighttpd plugin
23 /* plugin config for all request/connections */
25 typedef struct {
26 array *exclude_ext;
27 unsigned short etags_used;
28 unsigned short disable_pathinfo;
29 } plugin_config;
31 typedef struct {
32 PLUGIN_DATA;
34 plugin_config **config_storage;
36 plugin_config conf;
37 } plugin_data;
39 /* init the plugin data */
40 INIT_FUNC(mod_staticfile_init) {
41 plugin_data *p;
43 p = calloc(1, sizeof(*p));
45 return p;
48 /* detroy the plugin data */
49 FREE_FUNC(mod_staticfile_free) {
50 plugin_data *p = p_d;
52 UNUSED(srv);
54 if (!p) return HANDLER_GO_ON;
56 if (p->config_storage) {
57 size_t i;
58 for (i = 0; i < srv->config_context->used; i++) {
59 plugin_config *s = p->config_storage[i];
61 if (NULL == s) continue;
63 array_free(s->exclude_ext);
65 free(s);
67 free(p->config_storage);
70 free(p);
72 return HANDLER_GO_ON;
75 /* handle plugin config and check values */
77 SETDEFAULTS_FUNC(mod_staticfile_set_defaults) {
78 plugin_data *p = p_d;
79 size_t i = 0;
81 config_values_t cv[] = {
82 { "static-file.exclude-extensions", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
83 { "static-file.etags", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
84 { "static-file.disable-pathinfo", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
85 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
88 if (!p) return HANDLER_ERROR;
90 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
92 for (i = 0; i < srv->config_context->used; i++) {
93 data_config const* config = (data_config const*)srv->config_context->data[i];
94 plugin_config *s;
96 s = calloc(1, sizeof(plugin_config));
97 s->exclude_ext = array_init();
98 s->etags_used = 1;
99 s->disable_pathinfo = 0;
101 cv[0].destination = s->exclude_ext;
102 cv[1].destination = &(s->etags_used);
103 cv[2].destination = &(s->disable_pathinfo);
105 p->config_storage[i] = s;
107 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
108 return HANDLER_ERROR;
111 if (!array_is_vlist(s->exclude_ext)) {
112 log_error_write(srv, __FILE__, __LINE__, "s",
113 "unexpected value for static-file.exclude-extensions; expected list of \"ext\"");
114 return HANDLER_ERROR;
118 return HANDLER_GO_ON;
121 #define PATCH(x) \
122 p->conf.x = s->x;
123 static int mod_staticfile_patch_connection(server *srv, connection *con, plugin_data *p) {
124 size_t i, j;
125 plugin_config *s = p->config_storage[0];
127 PATCH(exclude_ext);
128 PATCH(etags_used);
129 PATCH(disable_pathinfo);
131 /* skip the first, the global context */
132 for (i = 1; i < srv->config_context->used; i++) {
133 data_config *dc = (data_config *)srv->config_context->data[i];
134 s = p->config_storage[i];
136 /* condition didn't match */
137 if (!config_check_cond(srv, con, dc)) continue;
139 /* merge config */
140 for (j = 0; j < dc->value->used; j++) {
141 data_unset *du = dc->value->data[j];
143 if (buffer_is_equal_string(du->key, CONST_STR_LEN("static-file.exclude-extensions"))) {
144 PATCH(exclude_ext);
145 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("static-file.etags"))) {
146 PATCH(etags_used);
147 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("static-file.disable-pathinfo"))) {
148 PATCH(disable_pathinfo);
153 return 0;
155 #undef PATCH
157 URIHANDLER_FUNC(mod_staticfile_subrequest) {
158 plugin_data *p = p_d;
159 size_t k;
160 data_string *ds;
162 /* someone else has done a decision for us */
163 if (con->http_status != 0) return HANDLER_GO_ON;
164 if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
165 if (buffer_is_empty(con->physical.path)) return HANDLER_GO_ON;
167 /* someone else has handled this request */
168 if (con->mode != DIRECT) return HANDLER_GO_ON;
170 /* we only handle GET, POST and HEAD */
171 switch(con->request.http_method) {
172 case HTTP_METHOD_GET:
173 case HTTP_METHOD_POST:
174 case HTTP_METHOD_HEAD:
175 break;
176 default:
177 return HANDLER_GO_ON;
180 mod_staticfile_patch_connection(srv, con, p);
182 if (p->conf.disable_pathinfo && !buffer_string_is_empty(con->request.pathinfo)) {
183 if (con->conf.log_request_handling) {
184 log_error_write(srv, __FILE__, __LINE__, "s", "-- NOT handling file as static file, pathinfo forbidden");
186 return HANDLER_GO_ON;
189 /* ignore certain extensions */
190 for (k = 0; k < p->conf.exclude_ext->used; k++) {
191 ds = (data_string *)p->conf.exclude_ext->data[k];
193 if (buffer_is_empty(ds->value)) continue;
195 if (buffer_is_equal_right_len(con->physical.path, ds->value, buffer_string_length(ds->value))) {
196 if (con->conf.log_request_handling) {
197 log_error_write(srv, __FILE__, __LINE__, "s", "-- NOT handling file as static file, extension forbidden");
199 return HANDLER_GO_ON;
204 if (con->conf.log_request_handling) {
205 log_error_write(srv, __FILE__, __LINE__, "s", "-- handling file as static file");
208 if (!p->conf.etags_used) con->etag_flags = 0;
209 http_response_send_file(srv, con, con->physical.path);
211 return HANDLER_FINISHED;
214 /* this function is called at dlopen() time and inits the callbacks */
216 int mod_staticfile_plugin_init(plugin *p);
217 int mod_staticfile_plugin_init(plugin *p) {
218 p->version = LIGHTTPD_VERSION_ID;
219 p->name = buffer_init_string("staticfile");
221 p->init = mod_staticfile_init;
222 p->handle_subrequest_start = mod_staticfile_subrequest;
223 p->set_defaults = mod_staticfile_set_defaults;
224 p->cleanup = mod_staticfile_free;
226 p->data = NULL;
228 return 0;