This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparamtable.c
319 lines (251 loc) · 8.62 KB
/
paramtable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* paramtable.c - The Apache::ParamTable class
* $Id$
*
* Author: Michael Granger <[email protected]>
* Copyright (c) 2003 RubyCrafters, LLC. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include "mod_ruby.h"
#include "apachelib.h"
VALUE rb_cApacheParamTable;
static ID atargs_id;
VALUE rb_apache_paramtable_new(table *tbl)
{
return Data_Wrap_Struct(rb_cApacheParamTable, NULL, NULL, tbl);
}
/*
* Object validity checker. Returns the data pointer.
*/
static table *check_paramtable( VALUE self )
{
Check_Type( self, T_DATA );
if ( !rb_obj_is_instance_of(self, rb_cApacheParamTable) ) {
rb_raise( rb_eTypeError,
"wrong argument type %s (expected Apache::ParamTable)",
rb_class2name(CLASS_OF( self )) );
}
return DATA_PTR( self );
}
/*
* Fetch the data pointer and check it for sanity.
*/
static table *get_paramtable( VALUE self )
{
table *ptr = check_paramtable( self );
if ( !ptr )
rb_raise( rb_eRuntimeError, "uninitialized Apache::ParamTable" );
return ptr;
}
/* Instance methods */
/*
* The code below is largely code from mod_ruby/table.c hacked to accept and
* return Apache::MultiVals instead of Strings.
*/
/*
* clear()
* --
* Clear the parameter table.
*/
static VALUE paramtable_clear( VALUE self )
{
table *tbl = get_paramtable( self );
apr_table_clear( tbl );
return Qnil;
}
/*
* Convert the specified +val+ to a tainted String object and push it onto the
* given +ary+. Called as the block of a hash iterator by parameter-fetching
* methods. The +key+ parameter is ignored because the iteration is done over
* all values associated with the same key -- it exists only to appease
* ap_table_do().
*/
static int rb_ary_tainted_push( void *ary, const char *key, const char *val )
{
rb_ary_push( *(VALUE *)ary, rb_tainted_str_new2(val) );
return 1;
}
/*
* get( name )
* --
* Fetch the parameter with the specified name, or +nil+ if no such parameter
* exists. The +name+ is case-insensitive.
*/
static VALUE paramtable_get( VALUE self, VALUE name )
{
table *tbl = get_paramtable( self );
const char *key;
VALUE res = Qnil;
key = StringValuePtr( name );
if ( apr_table_get(tbl, key) ) {
VALUE ary;
res = rb_class_new_instance(0, 0, rb_cApacheMultiVal);
ary = rb_ivar_get( res, atargs_id );
rb_ary_clear( ary );
apr_table_do( rb_ary_tainted_push, &ary, tbl, key, NULL );
}
return res;
}
/*
* set( name, val )
* --
* Set the parameter with the specified +name+ to the given +val+.
*/
static VALUE paramtable_set( VALUE self, VALUE name, VALUE val )
{
table *tbl = get_paramtable( self );
const char* key = StringValuePtr( name );
if ( rb_obj_is_instance_of(val, rb_cApacheMultiVal) ) {
VALUE ary = rb_ivar_get( val, atargs_id );
VALUE str;
int i;
apr_table_unset( tbl, key );
for ( i = 0; i < RARRAY_LEN(ary); i++ ) {
str = rb_check_convert_type( *(RARRAY_PTR(ary)+i), T_STRING,
"String", "to_str" );
apr_table_add( tbl, key, StringValuePtr(str) );
}
}
else {
val = rb_check_convert_type( val, T_STRING, "String", "to_str" );
apr_table_set( tbl, key, StringValuePtr(val) );
}
return val;
}
/*
* unset( name )
* --
* Clear the parameter with the given +name+ from the parameter table.
*/
static VALUE paramtable_unset( VALUE self, VALUE name )
{
table *tbl = get_paramtable( self );
apr_table_unset( tbl, StringValuePtr(name) );
return Qnil;
}
/*
* each {|multival| ...}
* --
* Iterate over the parameters in the table, passing the specified block a key
* and an Apache::MultiVal for each one.
*/
static VALUE paramtable_each( VALUE self )
{
table *tbl = get_paramtable( self );
VALUE key, val, ary, assoc, res;
const array_header *hdrs_arr;
table_entry *hdrs;
int i;
hdrs_arr = apr_table_elts( tbl );
hdrs = (table_entry *) hdrs_arr->elts;
res = rb_ary_new2( hdrs_arr->nelts + 1 );
for ( i = 0; i < hdrs_arr->nelts; i++ ) {
if ( hdrs[i].key == NULL )
continue;
key = rb_tainted_str_new2( hdrs[i].key );
val = rb_class_new_instance(0, 0, rb_cApacheMultiVal);
ary = rb_ivar_get( val, atargs_id );
rb_ary_clear( ary );
apr_table_do( rb_ary_tainted_push, &ary, tbl, hdrs[i].key, NULL );
assoc = rb_assoc_new( key, val );
rb_yield( assoc );
rb_ary_store( res, i, assoc );
}
return res;
}
/*
* keys
* --
* Returns an Array of the names of all the parameters in the param table.
*/
static VALUE paramtable_keys( VALUE self )
{
table *tbl = get_paramtable( self );
const array_header *hdrs_arr;
table_entry *hdrs;
int i;
VALUE res;
hdrs_arr = apr_table_elts( tbl );
hdrs = (table_entry *) hdrs_arr->elts;
res = rb_ary_new2( hdrs_arr->nelts + 1 );
for ( i = 0; i < hdrs_arr->nelts; i++ ) {
if ( hdrs[i].key == NULL ) continue;
rb_ary_store( res, i, rb_tainted_str_new2(hdrs[i].key) );
}
return res;
}
/*
* values
* --
* Returns an array of the values of the parameters (Apache::MultiVal objects).
*/
static VALUE paramtable_values( VALUE self )
{
table *tbl = get_paramtable( self );
VALUE key, val, ary, res;
const array_header *hdrs_arr;
table_entry *hdrs;
int i;
hdrs_arr = apr_table_elts( tbl );
hdrs = (table_entry *) hdrs_arr->elts;
res = rb_ary_new2( hdrs_arr->nelts + 1 );
for ( i = 0; i < hdrs_arr->nelts; i++ ) {
if ( hdrs[i].key == NULL )
continue;
key = rb_tainted_str_new2( hdrs[i].key );
val = rb_class_new_instance(0, 0, rb_cApacheMultiVal);
ary = rb_ivar_get( val, atargs_id );
rb_ary_clear( ary );
apr_table_do( rb_ary_tainted_push, &ary, tbl, hdrs[i].key, NULL );
rb_ary_store( res, i, val );
}
return res;
}
/* --- End of stuff from table.c ------------------------------ */
/* Module initializer */
void rb_init_apache_paramtable()
{
atargs_id = rb_intern( "@args" );
/* Kluge to make Rdoc see the associations in this file */
#if FOR_RDOC_PARSER
rb_mApache = rb_define_module( "Apache" );
rb_cApacheRequest = rb_define_class_under( rb_mApache, "Request", rb_cObject );
rb_cApacheTable = rb_define_class_under( rb_mApache, "Table", rb_cObject );
rb_cApacheMultiVal = rb_define_class_under( rb_mApache, "MultiVal", rb_cObject );
#endif
rb_cApacheParamTable = rb_define_class_under( rb_mApache, "ParamTable", rb_cApacheTable );
/* Remove the constructor */
rb_undef_method ( CLASS_OF(rb_cApacheParamTable), "new" );
/* Instance methods */
rb_define_method( rb_cApacheParamTable, "clear", paramtable_clear, 0 );
rb_define_method( rb_cApacheParamTable, "get", paramtable_get, 1 );
rb_define_alias ( rb_cApacheParamTable, "[]", "get");
rb_define_method( rb_cApacheParamTable, "set", paramtable_set, 2 );
rb_define_alias ( rb_cApacheParamTable, "[]=", "set" );
rb_define_method( rb_cApacheParamTable, "unset", paramtable_unset, 1 );
rb_define_method( rb_cApacheParamTable, "each", paramtable_each, 0 );
rb_define_method( rb_cApacheParamTable, "keys", paramtable_keys, 0 );
rb_define_method( rb_cApacheParamTable, "values", paramtable_values, 0 );
}
/* vim: set filetype=c ts=8 sw=4 : */