10 #include "http_chunk.h"
17 * this is a staticfile for a lighttpd plugin
23 /* plugin config for all request/connections */
27 unsigned short etags_used
;
28 unsigned short disable_pathinfo
;
34 plugin_config
**config_storage
;
39 /* init the plugin data */
40 INIT_FUNC(mod_staticfile_init
) {
43 p
= calloc(1, sizeof(*p
));
48 /* detroy the plugin data */
49 FREE_FUNC(mod_staticfile_free
) {
54 if (!p
) return HANDLER_GO_ON
;
56 if (p
->config_storage
) {
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
);
67 free(p
->config_storage
);
75 /* handle plugin config and check values */
77 SETDEFAULTS_FUNC(mod_staticfile_set_defaults
) {
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
];
96 s
= calloc(1, sizeof(plugin_config
));
97 s
->exclude_ext
= array_init();
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
;
123 static int mod_staticfile_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
125 plugin_config
*s
= p
->config_storage
[0];
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;
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"))) {
145 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("static-file.etags"))) {
147 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("static-file.disable-pathinfo"))) {
148 PATCH(disable_pathinfo
);
157 URIHANDLER_FUNC(mod_staticfile_subrequest
) {
158 plugin_data
*p
= p_d
;
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
:
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
;