~ubuntu-branches/ubuntu/trusty/libapache2-mod-rivet/trusty

« back to all changes in this revision

Viewing changes to src/apache-2/rivetConf.c

  • Committer: Package Import Robot
  • Author(s): Massimo Manghi
  • Date: 2013-10-02 11:44:17 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20131002114417-aimbnyf22r39iv39
Tags: 2.1.3-1
* New upstream code 2.1.3 
* Removed package dh-apache2 from Build-Depends
* Undone patch removing distclean target from doc/Makefile.am

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * rivetConf.c - Functions for accessing Rivet configuration variables
3
 
 *
4
 
 * Functions in this file implement core function to be called mainly
5
 
 * by the Rivet_InspectCmd function, which implments command 'inspect'
6
 
 *
7
 
 */
8
 
 
9
 
/* Copyright 2002-2004 The Apache Software Foundation
10
 
 
11
 
   Licensed under the Apache License, Version 2.0 (the "License");
12
 
   you may not use this file except in compliance with the License.
13
 
   You may obtain a copy of the License at
14
 
 
15
 
        http://www.apache.org/licenses/LICENSE-2.0
16
 
 
17
 
   Unless required by applicable law or agreed to in writing, software
18
 
   distributed under the License is distributed on an "AS IS" BASIS,
19
 
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 
   See the License for the specific language governing permissions and
21
 
   limitations under the License.
22
 
*/
23
 
 
24
 
/* $Id: */
25
 
 
26
 
#include <tcl.h>
27
 
#include <string.h>
28
 
#include <apr_errno.h>
29
 
#include <apr_strings.h>
30
 
#include <apr_tables.h>
31
 
 
32
 
#include "httpd.h"
33
 
#include "http_config.h"
34
 
#include "http_request.h"
35
 
#include "http_core.h"
36
 
#include "http_main.h"
37
 
#include "util_script.h"
38
 
#include "http_config.h"
39
 
 
40
 
#include "mod_rivet.h"
41
 
 
42
 
static const char* confDirectives[] = 
43
 
44
 
                    "ServerInitScript", 
45
 
                    "GlobalInitScript",
46
 
                    "ChildInitScript",
47
 
                    "ChildExitScript",
48
 
                    "BeforeScript",
49
 
                    "AfterScript",
50
 
                    "AfterEveryScript",
51
 
                    "AbortScript",
52
 
                    "ErrorScript",
53
 
                    "UploadMaxSize",
54
 
                    "UploadDirectory",
55
 
                    "UploadFilesToVar",
56
 
                    "SeparateVirtualInterps",
57
 
                    "HonorHeaderOnlyRequests",
58
 
                    NULL 
59
 
};
60
 
 
61
 
enum confIndices {
62
 
                    server_init_script,
63
 
                    global_init_script,
64
 
                    child_init_script,
65
 
                    child_exit_script,
66
 
                    before_script,
67
 
                    after_script,
68
 
                    after_every_script,
69
 
                    abort_script,
70
 
                    error_script,
71
 
                    upload_max,
72
 
                    upload_directory,
73
 
                    upload_files_to_var,
74
 
                    separate_virtual_interps,
75
 
                    honor_header_only_requests,
76
 
                    conf_index_terminator 
77
 
};
78
 
 
79
 
/* 
80
 
 * -- Rivet_ReadConfParameter
81
 
 *
82
 
 * This procedure reads a single field named par_name from 
83
 
 * rivet_server_conf structure and returns a Tcl_Obj pointer
84
 
 * containing the field value. See confDirectives for a list
85
 
 * of possible names. If the procedure is queried for a non  
86
 
 * existing field a NULL is returned.
87
 
 *
88
 
 *  Arguments:
89
 
 *
90
 
 *  - interp: pointer to the current Tcl interpreter structure
91
 
 *  - rsc: a pointer to a rivet_server_conf structure
92
 
 *  - par_name: parameter name (as listed in confDirectives)
93
 
 *
94
 
 * Returned value:
95
 
 *
96
 
 *  - A Tcl_Obj pointer to the parameter value whose refCount is
97
 
 *    set to 1 before returning to the caller
98
 
 *
99
 
 */
100
 
 
101
 
Tcl_Obj*
102
 
Rivet_ReadConfParameter ( Tcl_Interp* interp,
103
 
                          rivet_server_conf* rsc,
104
 
                          Tcl_Obj*    par_name)
105
 
{
106
 
    int parameter_i;
107
 
    Tcl_Obj* par_value;
108
 
 
109
 
    if (Tcl_GetIndexFromObj(interp, par_name, confDirectives,
110
 
                "<one of conf directives>", 0, &parameter_i) == TCL_ERROR) {
111
 
        return NULL;
112
 
    }
113
 
 
114
 
    switch (parameter_i)
115
 
    {
116
 
        case server_init_script:
117
 
        {
118
 
            par_value = rsc->rivet_server_init_script;
119
 
            break;
120
 
        }
121
 
        case global_init_script:
122
 
        {
123
 
            par_value = rsc->rivet_global_init_script;
124
 
            break;
125
 
        }
126
 
        case child_init_script:
127
 
        {
128
 
            par_value = rsc->rivet_child_init_script;
129
 
            break;
130
 
        }
131
 
        case child_exit_script:
132
 
        {
133
 
            par_value = rsc->rivet_child_exit_script;
134
 
            break;
135
 
        }
136
 
        case before_script:
137
 
        {
138
 
            par_value = rsc->rivet_before_script;
139
 
            break;
140
 
        }
141
 
        case after_script:
142
 
        {
143
 
            par_value = rsc->rivet_after_script;
144
 
            break;
145
 
        }
146
 
        case after_every_script:
147
 
        {
148
 
            par_value = rsc->after_every_script;
149
 
            break;
150
 
        }
151
 
        case abort_script:
152
 
        {
153
 
            par_value = rsc->rivet_abort_script;
154
 
            break;
155
 
        }
156
 
        case error_script:
157
 
        {
158
 
            par_value = rsc->rivet_error_script;
159
 
            break;
160
 
        }
161
 
        case upload_max:
162
 
        {
163
 
            par_value = Tcl_NewIntObj(rsc->upload_max);
164
 
            break;
165
 
        }
166
 
        case upload_directory:
167
 
        {
168
 
            par_value = Tcl_NewStringObj(rsc->upload_dir,-1);
169
 
            break;
170
 
        }
171
 
        case upload_files_to_var:
172
 
        {
173
 
            par_value = Tcl_NewIntObj(rsc->upload_files_to_var);
174
 
            break;
175
 
        }
176
 
        case separate_virtual_interps:
177
 
        {
178
 
            par_value = Tcl_NewIntObj(rsc->separate_virtual_interps);
179
 
            break;
180
 
        }
181
 
        case honor_header_only_requests:
182
 
        {
183
 
            par_value = Tcl_NewIntObj(rsc->honor_header_only_reqs);
184
 
            break;
185
 
        }
186
 
        default:
187
 
        {
188
 
            return NULL;
189
 
        }
190
 
    }
191
 
 
192
 
    if (par_value == NULL) 
193
 
    {
194
 
        par_value=Tcl_NewStringObj("<undef>",-1);
195
 
    }
196
 
 
197
 
    Tcl_IncrRefCount(par_value);
198
 
 
199
 
    return par_value;
200
 
}
201
 
 
202
 
/* 
203
 
 * Rivet_ReadConfTable: 
204
 
 * 
205
 
 * This procedure builds a key-value list from an apr table
206
 
 * It's called by Rivet_BuildConfDictionary to read theRivet 
207
 
 * configuration tables but it can work for every apr table
208
 
 *
209
 
 * Arguments:
210
 
 *
211
 
 *  - interp: Tcl_Interp pointer
212
 
 *  - table: an apr_table_t pointer
213
 
 *
214
 
 */
215
 
 
216
 
Tcl_Obj* Rivet_ReadConfTable (  Tcl_Interp*           interp,
217
 
                                apr_table_t*          table)
218
 
{
219
 
    Tcl_Obj*                key;
220
 
    Tcl_Obj*                val;
221
 
    apr_array_header_t      *arr;
222
 
    apr_table_entry_t       *elts;
223
 
    int                     nelts,i;
224
 
    int                     tcl_status = TCL_OK;
225
 
    Tcl_Obj*                keyval_list = Tcl_NewObj();
226
 
 
227
 
    Tcl_IncrRefCount(keyval_list);
228
 
 
229
 
    arr   = (apr_array_header_t*) apr_table_elts( table );
230
 
    elts  = (apr_table_entry_t *) arr->elts;
231
 
    nelts = arr->nelts;
232
 
 
233
 
/*
234
 
    if (Tcl_IsShared(keyval_list))
235
 
    {
236
 
        fprintf(stderr,"building duplicate keyval_list\n");
237
 
        keyval_list = Tcl_DuplicateObj(keyval_list);
238
 
    }
239
 
 */
240
 
 
241
 
    for (i = 0; i < nelts; i++)
242
 
    {
243
 
        key = Tcl_NewStringObj( elts[i].key, -1);
244
 
        val = Tcl_NewStringObj( elts[i].val, -1);
245
 
        Tcl_IncrRefCount(key);
246
 
        Tcl_IncrRefCount(val);
247
 
 
248
 
        tcl_status = Tcl_ListObjAppendElement (interp,keyval_list,key);
249
 
        if (tcl_status == TCL_ERROR)
250
 
        {   
251
 
            Tcl_DecrRefCount(keyval_list);
252
 
            Tcl_DecrRefCount(key);
253
 
            Tcl_DecrRefCount(val);
254
 
            return NULL;
255
 
        }
256
 
 
257
 
        tcl_status = Tcl_ListObjAppendElement (interp,keyval_list,val);
258
 
        if (tcl_status == TCL_ERROR)
259
 
        {
260
 
            Tcl_DecrRefCount(keyval_list);
261
 
            Tcl_DecrRefCount(key);
262
 
            Tcl_DecrRefCount(val);
263
 
            return NULL;
264
 
        }
265
 
 
266
 
        Tcl_DecrRefCount(key);
267
 
        Tcl_DecrRefCount(val);
268
 
    }
269
 
 
270
 
    return keyval_list;
271
 
}
272
 
 
273
 
 
274
 
/*
275
 
 * -- Rivet_BuildConfDictionary
276
 
 * 
277
 
 * Parameters set in the configuration files are collected in three
278
 
 * APR tables by Rivet_ServerConf,Rivet_DirConf and Rivet_UserConf. 
279
 
 *
280
 
 * Arguments:
281
 
 *
282
 
 * - interp: Tcl_Interp pointer
283
 
 * - rivet_conf: pointer to a rivet_server_conf structure as
284
 
 *   returned by Rivet_GetConf
285
 
 *
286
 
 * Returned value:
287
 
 *
288
 
 *  - Tcl dictionary storing the dir/user/server configuration. The
289
 
 *    dictionary refCount is incremented 
290
 
 *
291
 
 */
292
 
 
293
 
Tcl_Obj* Rivet_BuildConfDictionary ( Tcl_Interp*           interp,
294
 
                                     rivet_server_conf*    rivet_conf)
295
 
{
296
 
    apr_table_t* conf_tables[3];
297
 
    Tcl_Obj*     keyval_list = NULL;
298
 
    Tcl_Obj*     key_list[2];
299
 
    int          it;
300
 
    Tcl_Obj*     conf_dict = Tcl_NewObj();
301
 
 
302
 
    static const char* section_names[] = 
303
 
    {
304
 
        "dir",
305
 
        "user",
306
 
        "server"
307
 
    };
308
 
 
309
 
    enum
310
 
    {
311
 
        dir_conf_section,
312
 
        user_conf_section,
313
 
        server_conf_section
314
 
    };
315
 
 
316
 
    conf_tables[0] = rivet_conf->rivet_dir_vars;
317
 
    conf_tables[1] = rivet_conf->rivet_user_vars;
318
 
    conf_tables[2] = rivet_conf->rivet_server_vars;
319
 
 
320
 
    Tcl_IncrRefCount(conf_dict);
321
 
 
322
 
    for (it=0; it < 3; it++)
323
 
    {
324
 
        keyval_list = Rivet_ReadConfTable(interp,conf_tables[it]);
325
 
 
326
 
        if (keyval_list != NULL)
327
 
        {
328
 
            int       i;
329
 
            Tcl_Obj** objArrayPnt;
330
 
            int       objArrayCnt;
331
 
            Tcl_Obj*  val;
332
 
 
333
 
            key_list[0] = Tcl_NewStringObj(section_names[it],-1);
334
 
            Tcl_IncrRefCount(key_list[0]);
335
 
 
336
 
            Tcl_ListObjGetElements(interp,keyval_list,&objArrayCnt,&objArrayPnt);
337
 
            for (i=0; i < objArrayCnt; i+=2)
338
 
            {
339
 
                key_list[1] = objArrayPnt[i];
340
 
                val         = objArrayPnt[i+1];
341
 
 
342
 
                Tcl_IncrRefCount(key_list[1]);
343
 
                Tcl_IncrRefCount(val);
344
 
 
345
 
                Tcl_DictObjPutKeyList(interp,conf_dict,2,key_list,val);
346
 
 
347
 
                Tcl_DecrRefCount(key_list[1]);
348
 
                Tcl_DecrRefCount(val);
349
 
            }
350
 
            Tcl_DecrRefCount(key_list[0]);
351
 
            Tcl_DecrRefCount(keyval_list);
352
 
        }
353
 
        else
354
 
        {
355
 
            return NULL;
356
 
        }
357
 
    }
358
 
 
359
 
    return conf_dict;
360
 
}
361
 
 
362
 
 
363
 
/*
364
 
 * Rivet_CurrentConfDict 
365
 
 *
366
 
 * This function is called by Rivet_InspectCmd which implements command
367
 
 * '::rivet::inspect -all'. The function returns a dictionary where every
368
 
 * parameter name (confDirectives) is associated to its value stored in
369
 
 * the rivet_server_conf as returned by Rivet_GetConf
370
 
 *
371
 
 * Arguments:
372
 
 *
373
 
 * - interp: Tcl interpreter pointer
374
 
 * - rivet_conf: a pointer to a rivet_server_conf structure
375
 
 *
376
 
 * Returned value_
377
 
 *
378
 
 * - a Tcl_Obj* pointer to a dictionary. The dictionary object
379
 
 *  refCount is set to 1
380
 
 */
381
 
 
382
 
Tcl_Obj* Rivet_CurrentConfDict ( Tcl_Interp*           interp,
383
 
                                 rivet_server_conf*    rivet_conf)
384
 
{
385
 
    Tcl_Obj* dictObj = Tcl_NewObj();
386
 
    Tcl_Obj* par_name; 
387
 
    static const char** p;
388
 
 
389
 
    Tcl_IncrRefCount(dictObj);
390
 
 
391
 
    for (p = confDirectives; (*p) != NULL; p++)
392
 
    {
393
 
        Tcl_Obj* par_value;
394
 
 
395
 
        par_name = Tcl_NewStringObj(*p,-1);
396
 
        Tcl_IncrRefCount(par_name);
397
 
 
398
 
        par_value = Rivet_ReadConfParameter(interp,rivet_conf,par_name);
399
 
        if (par_value != NULL)
400
 
        {
401
 
            Tcl_DictObjPut(interp,dictObj,par_name,par_value);
402
 
            Tcl_DecrRefCount(par_value);
403
 
        }
404
 
        else
405
 
        {
406
 
            Tcl_Obj* message = Tcl_NewStringObj("Invalid configuration option: ",-1);
407
 
            
408
 
            Tcl_IncrRefCount(message);
409
 
            Tcl_AppendObjToObj(message,par_name);
410
 
            Tcl_AddErrorInfo(interp, Tcl_GetStringFromObj(message,NULL));
411
 
 
412
 
            Tcl_DecrRefCount(message);
413
 
            Tcl_DecrRefCount(par_name);
414
 
            Tcl_DecrRefCount(dictObj);
415
 
            dictObj = NULL;
416
 
            break;
417
 
        }
418
 
        Tcl_DecrRefCount(par_name);
419
 
 
420
 
    }
421
 
 
422
 
    return dictObj;
423
 
}