14 /* plugin config for all request/connections */
22 plugin_config
**config_storage
;
27 /* init the plugin data */
28 INIT_FUNC(mod_alias_init
) {
31 p
= calloc(1, sizeof(*p
));
38 /* detroy the plugin data */
39 FREE_FUNC(mod_alias_free
) {
42 if (!p
) return HANDLER_GO_ON
;
44 if (p
->config_storage
) {
47 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
48 plugin_config
*s
= p
->config_storage
[i
];
50 if (NULL
== s
) continue;
56 free(p
->config_storage
);
64 /* handle plugin config and check values */
66 SETDEFAULTS_FUNC(mod_alias_set_defaults
) {
70 config_values_t cv
[] = {
71 { "alias.url", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
72 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
75 if (!p
) return HANDLER_ERROR
;
77 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
79 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
80 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
83 s
= calloc(1, sizeof(plugin_config
));
84 s
->alias
= array_init();
85 cv
[0].destination
= s
->alias
;
87 p
->config_storage
[i
] = s
;
89 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
92 if (s
->alias
->used
>= 2) {
93 const array
*a
= s
->alias
;
96 for (j
= 0; j
< a
->used
; j
++) {
97 const buffer
*prefix
= a
->data
[a
->sorted
[j
]]->key
;
98 for (k
= j
+ 1; k
< a
->used
; k
++) {
99 const buffer
*key
= a
->data
[a
->sorted
[k
]]->key
;
101 if (buffer_string_length(key
) < buffer_string_length(prefix
)) {
104 if (memcmp(key
->ptr
, prefix
->ptr
, buffer_string_length(prefix
)) != 0) {
107 /* ok, they have same prefix. check position */
108 if (a
->sorted
[j
] < a
->sorted
[k
]) {
109 log_error_write(srv
, __FILE__
, __LINE__
, "SBSBS",
110 "url.alias: `", key
, "' will never match as `", prefix
, "' matched first");
111 return HANDLER_ERROR
;
118 return HANDLER_GO_ON
;
123 static int mod_alias_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
125 plugin_config
*s
= p
->config_storage
[0];
129 /* skip the first, the global context */
130 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
131 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
132 s
= p
->config_storage
[i
];
134 /* condition didn't match */
135 if (!config_check_cond(srv
, con
, dc
)) continue;
138 for (j
= 0; j
< dc
->value
->used
; j
++) {
139 data_unset
*du
= dc
->value
->data
[j
];
141 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("alias.url"))) {
151 PHYSICALPATH_FUNC(mod_alias_physical_handler
) {
152 plugin_data
*p
= p_d
;
153 int uri_len
, basedir_len
;
157 if (buffer_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
159 mod_alias_patch_connection(srv
, con
, p
);
161 /* not to include the tailing slash */
162 basedir_len
= buffer_string_length(con
->physical
.basedir
);
163 if ('/' == con
->physical
.basedir
->ptr
[basedir_len
-1]) --basedir_len
;
164 uri_len
= buffer_string_length(con
->physical
.path
) - basedir_len
;
165 uri_ptr
= con
->physical
.path
->ptr
+ basedir_len
;
167 for (k
= 0; k
< p
->conf
.alias
->used
; k
++) {
168 data_string
*ds
= (data_string
*)p
->conf
.alias
->data
[k
];
169 int alias_len
= buffer_string_length(ds
->key
);
171 if (alias_len
> uri_len
) continue;
172 if (buffer_is_empty(ds
->key
)) continue;
174 if (0 == (con
->conf
.force_lowercase_filenames
?
175 strncasecmp(uri_ptr
, ds
->key
->ptr
, alias_len
) :
176 strncmp(uri_ptr
, ds
->key
->ptr
, alias_len
))) {
179 buffer_copy_buffer(con
->physical
.basedir
, ds
->value
);
180 buffer_copy_buffer(srv
->tmp_buf
, ds
->value
);
181 buffer_append_string(srv
->tmp_buf
, uri_ptr
+ alias_len
);
182 buffer_copy_buffer(con
->physical
.path
, srv
->tmp_buf
);
184 return HANDLER_GO_ON
;
189 return HANDLER_GO_ON
;
192 /* this function is called at dlopen() time and inits the callbacks */
194 int mod_alias_plugin_init(plugin
*p
);
195 int mod_alias_plugin_init(plugin
*p
) {
196 p
->version
= LIGHTTPD_VERSION_ID
;
197 p
->name
= buffer_init_string("alias");
199 p
->init
= mod_alias_init
;
200 p
->handle_physical
= mod_alias_physical_handler
;
201 p
->set_defaults
= mod_alias_set_defaults
;
202 p
->cleanup
= mod_alias_free
;