14 /* plugin config for all request/connections */
17 int handled
; /* make sure that we only apply the headers once */
21 array
*request_header
;
22 array
*response_header
;
30 plugin_config
**config_storage
;
35 static handler_ctx
* handler_ctx_init(void) {
38 hctx
= calloc(1, sizeof(*hctx
));
45 static void handler_ctx_free(handler_ctx
*hctx
) {
50 /* init the plugin data */
51 INIT_FUNC(mod_setenv_init
) {
54 p
= calloc(1, sizeof(*p
));
59 /* detroy the plugin data */
60 FREE_FUNC(mod_setenv_free
) {
65 if (!p
) return HANDLER_GO_ON
;
67 if (p
->config_storage
) {
69 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
70 plugin_config
*s
= p
->config_storage
[i
];
72 if (NULL
== s
) continue;
74 array_free(s
->request_header
);
75 array_free(s
->response_header
);
76 array_free(s
->environment
);
80 free(p
->config_storage
);
88 /* handle plugin config and check values */
90 SETDEFAULTS_FUNC(mod_setenv_set_defaults
) {
94 config_values_t cv
[] = {
95 { "setenv.add-request-header", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
96 { "setenv.add-response-header", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
97 { "setenv.add-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
98 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
101 if (!p
) return HANDLER_ERROR
;
103 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
105 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
106 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
109 s
= calloc(1, sizeof(plugin_config
));
110 s
->request_header
= array_init();
111 s
->response_header
= array_init();
112 s
->environment
= array_init();
114 cv
[0].destination
= s
->request_header
;
115 cv
[1].destination
= s
->response_header
;
116 cv
[2].destination
= s
->environment
;
118 p
->config_storage
[i
] = s
;
120 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
121 return HANDLER_ERROR
;
125 return HANDLER_GO_ON
;
130 static int mod_setenv_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
132 plugin_config
*s
= p
->config_storage
[0];
134 PATCH(request_header
);
135 PATCH(response_header
);
138 /* skip the first, the global context */
139 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
140 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
141 s
= p
->config_storage
[i
];
143 /* condition didn't match */
144 if (!config_check_cond(srv
, con
, dc
)) continue;
147 for (j
= 0; j
< dc
->value
->used
; j
++) {
148 data_unset
*du
= dc
->value
->data
[j
];
150 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("setenv.add-request-header"))) {
151 PATCH(request_header
);
152 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("setenv.add-response-header"))) {
153 PATCH(response_header
);
154 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("setenv.add-environment"))) {
164 URIHANDLER_FUNC(mod_setenv_uri_handler
) {
165 plugin_data
*p
= p_d
;
169 if (con
->plugin_ctx
[p
->id
]) {
170 hctx
= con
->plugin_ctx
[p
->id
];
172 hctx
= handler_ctx_init();
174 con
->plugin_ctx
[p
->id
] = hctx
;
178 return HANDLER_GO_ON
;
183 mod_setenv_patch_connection(srv
, con
, p
);
185 for (k
= 0; k
< p
->conf
.request_header
->used
; k
++) {
186 data_string
*ds
= (data_string
*)p
->conf
.request_header
->data
[k
];
189 if (NULL
== (ds_dst
= (data_string
*)array_get_unused_element(con
->request
.headers
, TYPE_STRING
))) {
190 ds_dst
= data_string_init();
193 buffer_copy_buffer(ds_dst
->key
, ds
->key
);
194 buffer_copy_buffer(ds_dst
->value
, ds
->value
);
196 array_insert_unique(con
->request
.headers
, (data_unset
*)ds_dst
);
199 for (k
= 0; k
< p
->conf
.environment
->used
; k
++) {
200 data_string
*ds
= (data_string
*)p
->conf
.environment
->data
[k
];
203 if (NULL
== (ds_dst
= (data_string
*)array_get_unused_element(con
->environment
, TYPE_STRING
))) {
204 ds_dst
= data_string_init();
207 buffer_copy_buffer(ds_dst
->key
, ds
->key
);
208 buffer_copy_buffer(ds_dst
->value
, ds
->value
);
210 array_insert_unique(con
->environment
, (data_unset
*)ds_dst
);
213 for (k
= 0; k
< p
->conf
.response_header
->used
; k
++) {
214 data_string
*ds
= (data_string
*)p
->conf
.response_header
->data
[k
];
216 response_header_insert(srv
, con
, CONST_BUF_LEN(ds
->key
), CONST_BUF_LEN(ds
->value
));
220 return HANDLER_GO_ON
;
223 CONNECTION_FUNC(mod_setenv_reset
) {
224 plugin_data
*p
= p_d
;
228 if (con
->plugin_ctx
[p
->id
]) {
229 handler_ctx_free(con
->plugin_ctx
[p
->id
]);
230 con
->plugin_ctx
[p
->id
] = NULL
;
233 return HANDLER_GO_ON
;
236 /* this function is called at dlopen() time and inits the callbacks */
238 int mod_setenv_plugin_init(plugin
*p
);
239 int mod_setenv_plugin_init(plugin
*p
) {
240 p
->version
= LIGHTTPD_VERSION_ID
;
241 p
->name
= buffer_init_string("setenv");
243 p
->init
= mod_setenv_init
;
244 p
->handle_uri_clean
= mod_setenv_uri_handler
;
245 p
->set_defaults
= mod_setenv_set_defaults
;
246 p
->cleanup
= mod_setenv_free
;
248 p
->connection_reset
= mod_setenv_reset
;