-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathutil.c
More file actions
457 lines (392 loc) · 11.1 KB
/
util.c
File metadata and controls
457 lines (392 loc) · 11.1 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
* Copyright (C) 2000, 2001, 2013 Gregory Trubetskoy
* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Originally developed by Gregory Trubetskoy.
*
*
* util.c
*
*
* See accompanying documentation and source code comments
* for details.
*
*/
#include "mod_python.h"
/**
** tuple_from_array_header
**
* Given an array header return a tuple. The array elements
* assumed to be strings.
*/
PyObject * tuple_from_array_header(const apr_array_header_t *ah)
{
PyObject *t;
int i;
char **s;
if (ah == NULL)
t = PyTuple_New(0);
else {
t = PyTuple_New(ah->nelts);
s = (char **) ah->elts;
for (i = 0; i < ah->nelts; i++)
PyTuple_SetItem(t, i, MpBytesOrUnicode_FromString(s[i]));
}
return t;
}
/**
** tuple_from_method_list
**
* Given an apr_method_list_t return a tuple.
*/
PyObject * tuple_from_method_list(const ap_method_list_t *l)
{
PyObject *t;
int i;
char **methods;
if ((l->method_list == NULL) || (l->method_list->nelts == 0))
t = PyTuple_New(0);
else {
t = PyTuple_New(l->method_list->nelts);
methods = (char **)l->method_list->elts;
for (i = 0; i < l->method_list->nelts; ++i)
PyTuple_SetItem(t, i, MpBytesOrUnicode_FromString(methods[i]));
}
return t;
}
/**
** tuple_from_finfo
**
* makes a tuple similar to return of os.stat() from apr_finfo_t
*
*/
PyObject *tuple_from_finfo(apr_finfo_t *f)
{
PyObject *t;
if (f->filetype == APR_NOFILE) {
Py_INCREF(Py_None);
return Py_None;
}
t = PyTuple_New(13);
/* this should have been first, but was added later */
PyTuple_SET_ITEM(t, 12, PyLong_FromLong(f->filetype));
if (f->valid & APR_FINFO_PROT) {
PyTuple_SET_ITEM(t, 0, PyLong_FromLong(f->protection));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 0, Py_None);
}
if (f->valid & APR_FINFO_INODE) {
PyTuple_SET_ITEM(t, 1, PyLong_FromLong(f->inode));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 1, Py_None);
}
if (f->valid & APR_FINFO_DEV) {
PyTuple_SET_ITEM(t, 2, PyLong_FromLong(f->device));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 2, Py_None);
}
if (f->valid & APR_FINFO_NLINK) {
PyTuple_SET_ITEM(t, 3, PyLong_FromLong(f->nlink));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 3, Py_None);
}
if (f->valid & APR_FINFO_USER) {
PyTuple_SET_ITEM(t, 4, PyLong_FromLong(f->user));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 4, Py_None);
}
if (f->valid & APR_FINFO_GROUP) {
PyTuple_SET_ITEM(t, 5, PyLong_FromLong(f->group));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 5, Py_None);
}
if (f->valid & APR_FINFO_SIZE) {
PyTuple_SET_ITEM(t, 6, PyLong_FromLong(f->size));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 6, Py_None);
}
if (f->valid & APR_FINFO_ATIME) {
PyTuple_SET_ITEM(t, 7, PyLong_FromLong(f->atime*0.000001));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 7, Py_None);
}
if (f->valid & APR_FINFO_MTIME) {
PyTuple_SET_ITEM(t, 8, PyLong_FromLong(f->mtime*0.000001));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 8, Py_None);
}
if (f->valid & APR_FINFO_CTIME) {
PyTuple_SET_ITEM(t, 9, PyLong_FromLong(f->ctime*0.000001));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 9, Py_None);
}
if (f->fname) {
PyTuple_SET_ITEM(t, 10, MpBytesOrUnicode_FromString(f->fname));
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 10, Py_None);
}
if (f->valid & APR_FINFO_NAME) {
PyTuple_SET_ITEM(t, 11, MpBytesOrUnicode_FromString(f->name));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 11, Py_None);
}
/* it'd be nice to also return the file dscriptor,
f->filehand->filedes, but it's platform dependent,
so may be later... */
return t;
}
/**
** tuple_from_apr_uri
**
* makes a tuple from uri_components
*
*/
PyObject *tuple_from_apr_uri(apr_uri_t *u)
{
PyObject *t;
t = PyTuple_New(9);
if (u->scheme) {
PyTuple_SET_ITEM(t, 0, MpBytesOrUnicode_FromString(u->scheme));
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 0, Py_None);
}
if (u->hostinfo) {
PyTuple_SET_ITEM(t, 1, MpBytesOrUnicode_FromString(u->hostinfo));
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 1, Py_None);
}
if (u->user) {
PyTuple_SET_ITEM(t, 2, MpBytesOrUnicode_FromString(u->user));
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 2, Py_None);
}
if (u->password) {
PyTuple_SET_ITEM(t, 3, MpBytesOrUnicode_FromString(u->password));
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 3, Py_None);
}
if (u->hostname) {
PyTuple_SET_ITEM(t, 4, MpBytesOrUnicode_FromString(u->hostname));
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 4, Py_None);
}
if (u->port_str) {
PyTuple_SET_ITEM(t, 5, PyLong_FromLong(u->port));
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 5, Py_None);
}
if (u->path) {
PyTuple_SET_ITEM(t, 6, MpBytesOrUnicode_FromString(u->path));
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 6, Py_None);
}
if (u->query) {
PyTuple_SET_ITEM(t, 7, MpBytesOrUnicode_FromString(u->query));
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 7, Py_None);
}
if (u->fragment) {
PyTuple_SET_ITEM(t, 8, MpBytesOrUnicode_FromString(u->fragment));
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 8, Py_None);
}
/* XXX hostent, is_initialized, dns_* */
return t;
}
/**
** python_decref
**
* This helper function is used with apr_pool_cleanup_register to destroy
* python objects when a certain pool is destroyed.
*/
apr_status_t python_decref(void *object)
{
Py_XDECREF((PyObject *) object);
return 0;
}
/**
** find_module
**
* Find an Apache module by name, used by get_addhandler_extensions
*/
static module *find_module(char *name)
{
int n;
for (n = 0; ap_loaded_modules[n]; ++n) {
if (strcmp(name, ap_loaded_modules[n]->name) == 0)
return ap_loaded_modules[n];
}
return NULL;
}
/**
** get_addhandler_extensions
**
* Get extensions specified for AddHandler, if any. To do this we
* retrieve mod_mime's config. This is used by the publisher to strip
* file extentions from modules in the most meaningful way.
*
* XXX This function is a hack and will stop working if mod_mime people
* decide to change their code. A better way to implement this would
* be via the config tree, but it doesn't seem to be quite there just
* yet, because it doesn't have .htaccess directives.
*/
char * get_addhandler_extensions(request_rec *req)
{
/* these typedefs are copied from mod_mime.c */
typedef struct {
apr_hash_t *extension_mappings;
apr_array_header_t *remove_mappings;
char *default_language;
int multimatch;
} mime_dir_config;
typedef struct extension_info {
char *forced_type; /* Additional AddTyped stuff */
char *encoding_type; /* Added with AddEncoding... */
char *language_type; /* Added with AddLanguage... */
char *handler; /* Added with AddHandler... */
char *charset_type; /* Added with AddCharset... */
char *input_filters; /* Added with AddInputFilter... */
char *output_filters; /* Added with AddOutputFilter... */
} extension_info;
mime_dir_config *mconf;
apr_hash_index_t *hi;
void *val;
void *key;
extension_info *ei;
char *result = NULL;
module *mod_mime = find_module("mod_mime.c");
mconf = (mime_dir_config *) ap_get_module_config(req->per_dir_config, mod_mime);
if (mconf->extension_mappings) {
for (hi = apr_hash_first(req->pool, mconf->extension_mappings); hi; hi = apr_hash_next(hi)) {
apr_hash_this(hi, (const void **)&key, NULL, &val);
ei = (extension_info *)val;
if (ei->handler)
if (strcmp("mod_python", ei->handler) == 0 ||
strcmp("python-program", ei->handler) == 0)
result = apr_pstrcat(req->pool, (char *)key, " ", result, NULL);
}
}
return result;
}
/**
** find_memberdef
**
* Find a memberdef in a PyMemberDef array
*/
PyMemberDef *find_memberdef(const PyMemberDef *mlist, const char *name)
{
const PyMemberDef *md;
for (md = mlist; md->name != NULL; md++)
if (name[0] == md->name[0] &&
strcmp(md->name+1, name+1) == 0)
return (PyMemberDef *)md;
/* this should never happen or the mlist is screwed up */
return NULL;
}
/**
** cfgtree_walk
**
* walks ap_directive_t tree returning a list of
* tuples and lists
*/
PyObject *cfgtree_walk(ap_directive_t *dir)
{
PyObject *list = PyList_New(0);
if (!list)
return PyErr_NoMemory();
while (dir) {
PyObject *t = Py_BuildValue("(s, s)", dir->directive, dir->args);
if (!t)
return PyErr_NoMemory();
PyList_Append(list, t);
Py_DECREF(t);
if (dir->first_child) {
PyObject *child = cfgtree_walk(dir->first_child);
if (!child)
return PyErr_NoMemory();
PyList_Append(list, child);
Py_DECREF(child);
}
dir = dir->next;
}
return list;
}
/**
** makeipaddr
**
* utility func to make an ip address
*/
static PyObject *makeipaddr(struct apr_sockaddr_t *addr)
{
char *str = NULL;
apr_status_t rc;
PyObject *ret = NULL;
rc = apr_sockaddr_ip_get(&str, addr);
if (rc==APR_SUCCESS) {
ret = MpBytesOrUnicode_FromString(str);
}
else {
PyErr_SetString(PyExc_SystemError,"apr_sockaddr_ip_get failure");
}
return ret;
}
/**
** makesockaddr
**
* utility func to make a socket address
*/
PyObject *makesockaddr(struct apr_sockaddr_t *addr)
{
PyObject *addrobj = makeipaddr(addr);
PyObject *ret = NULL;
if (addrobj) {
apr_port_t port;
port = addr->port;
ret = Py_BuildValue("Oi", addrobj, port );
Py_DECREF(addrobj);
}
return ret;
}