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(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
;
160 /* someone else has done a decision for us */
161 if (con
->http_status
!= 0) return HANDLER_GO_ON
;
162 if (buffer_is_empty(con
->uri
.path
)) return HANDLER_GO_ON
;
163 if (buffer_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
165 /* someone else has handled this request */
166 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
168 /* we only handle GET, POST and HEAD */
169 switch(con
->request
.http_method
) {
170 case HTTP_METHOD_GET
:
171 case HTTP_METHOD_POST
:
172 case HTTP_METHOD_HEAD
:
175 return HANDLER_GO_ON
;
178 mod_staticfile_patch_connection(srv
, con
, p
);
180 if (p
->conf
.disable_pathinfo
&& !buffer_string_is_empty(con
->request
.pathinfo
)) {
181 if (con
->conf
.log_request_handling
) {
182 log_error_write(srv
, __FILE__
, __LINE__
, "s", "-- NOT handling file as static file, pathinfo forbidden");
184 return HANDLER_GO_ON
;
187 /* ignore certain extensions */
188 if (0 != p
->conf
.exclude_ext
->used
&& array_match_value_suffix(p
->conf
.exclude_ext
, con
->physical
.path
)) {
189 if (con
->conf
.log_request_handling
) {
190 log_error_write(srv
, __FILE__
, __LINE__
, "s", "-- NOT handling file as static file, extension forbidden");
192 return HANDLER_GO_ON
;
196 if (con
->conf
.log_request_handling
) {
197 log_error_write(srv
, __FILE__
, __LINE__
, "s", "-- handling file as static file");
200 if (!p
->conf
.etags_used
) con
->etag_flags
= 0;
201 http_response_send_file(srv
, con
, con
->physical
.path
);
203 return HANDLER_FINISHED
;
206 /* this function is called at dlopen() time and inits the callbacks */
208 int mod_staticfile_plugin_init(plugin
*p
);
209 int mod_staticfile_plugin_init(plugin
*p
) {
210 p
->version
= LIGHTTPD_VERSION_ID
;
211 p
->name
= buffer_init_string("staticfile");
213 p
->init
= mod_staticfile_init
;
214 p
->handle_subrequest_start
= mod_staticfile_subrequest
;
215 p
->set_defaults
= mod_staticfile_set_defaults
;
216 p
->cleanup
= mod_staticfile_free
;