-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathrequestobject.c
More file actions
2432 lines (2028 loc) · 74.5 KB
/
requestobject.c
File metadata and controls
2432 lines (2028 loc) · 74.5 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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
*
*
* requestobject.c
*
*
*/
#include "mod_python.h"
/* mod_ssl.h is not safe for inclusion in 2.0, so duplicate the
* optional function declarations. */
APR_DECLARE_OPTIONAL_FN(char *, ssl_var_lookup,
(apr_pool_t *, server_rec *,
conn_rec *, request_rec *,
char *));
APR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *));
/* Optional functions imported from mod_ssl when loaded: */
static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *optfn_ssl_var_lookup = NULL;
static APR_OPTIONAL_FN_TYPE(ssl_is_https) *optfn_is_https = NULL;
/**
** MpRequest_FromRequest
**
* This routine creates a Python requestobject given an Apache
* request_rec pointer.
*
*/
PyObject * MpRequest_FromRequest(request_rec *req)
{
requestobject *result;
result = PyObject_GC_New(requestobject, &MpRequest_Type);
if (! result)
return PyErr_NoMemory();
result->dict = PyDict_New();
if (!result->dict)
return PyErr_NoMemory();
result->request_rec = req;
result->connection = NULL;
result->server = NULL;
result->headers_in = NULL;
result->headers_out = NULL;
result->err_headers_out = NULL;
result->subprocess_env = NULL;
result->notes = NULL;
result->phase = NULL;
result->config = NULL;
result->options = NULL;
result->extension = NULL;
result->content_type_set = 0;
result->bytes_queued = 0;
result->hlo = NULL;
result->rbuff = NULL;
result->rbuff_pos = 0;
result->rbuff_len = 0;
/* we make sure that the object dictionary is there
* before registering the object with the GC
*/
PyObject_GC_Track(result);
return (PyObject *) result;
}
/* Methods */
/**
** request.add_common_vars(reqeust self)
**
* Interface to ap_add_common_vars. Adds a some more of CGI
* environment variables to subprocess_env.
*
*/
static PyObject * req_add_common_vars(requestobject *self)
{
ap_add_common_vars(self->request_rec);
Py_INCREF(Py_None);
return Py_None;
}
/**
** request.add_cgi_vars(reqeust self)
**
* This is a clone of ap_add_cgi_vars which does not bother
* calculating PATH_TRANSLATED and thus avoids creating
* sub-requests and filesystem calls.
*
*/
static PyObject * req_add_cgi_vars(requestobject *self)
{
request_rec *r = self->request_rec;
apr_table_t *e = r->subprocess_env;
apr_table_setn(e, "GATEWAY_INTERFACE", "CGI/1.1");
apr_table_setn(e, "SERVER_PROTOCOL", r->protocol);
apr_table_setn(e, "REQUEST_METHOD", r->method);
apr_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
apr_table_setn(e, "REQUEST_URI", r->uri);
if (!r->path_info || !*r->path_info) {
apr_table_setn(e, "SCRIPT_NAME", r->uri);
}
else {
int path_info_start = ap_find_path_info(r->uri, r->path_info);
apr_table_setn(e, "SCRIPT_NAME",
apr_pstrndup(r->pool, r->uri, path_info_start));
apr_table_setn(e, "PATH_INFO", r->path_info);
}
ap_add_common_vars(self->request_rec);
Py_INCREF(Py_None);
return Py_None;
}
/**
** set_wsgi_path_info(self)
**
* Set path_info the way wsgi likes it.
* Return: 0 == OK, 1 == bad base_uri, 2 == base_uri mismatch
*/
static int set_wsgi_path_info(requestobject *self)
{
py_config *conf =
(py_config *) ap_get_module_config(self->request_rec->per_dir_config,
&python_module);
const char *path_info = self->request_rec->uri;
const char *base_uri = apr_table_get(conf->options, "mod_python.wsgi.base_uri");
if (!base_uri && conf->d_is_location) {
/* Use Location as the base_uri, automatically adjust trailing slash */
char *bu = apr_pstrdup(self->request_rec->pool, conf->config_dir);
int last = strlen(bu) - 1;
if (*bu && bu[last] == '/')
bu[last] = '\0';
base_uri = bu;
} else if (base_uri && *base_uri) {
/* This base_uri was set by hand, enforce correctness */
if (base_uri[strlen(base_uri)-1] == '/') {
PyErr_SetString(PyExc_ValueError,
apr_psprintf(self->request_rec->pool,
"PythonOption 'mod_python.wsgi.base_uri' ('%s') must not end with '/'",
base_uri));
return 1;
}
}
if (base_uri && *base_uri) {
/* find end of base_uri match in r->uri, this will be our path_info */
while (*path_info && *base_uri && (*path_info == *base_uri)) {
path_info++;
base_uri++;
}
if (*base_uri) {
/* we have not reached end of base_uri, therefore
r->uri does not start with base_uri */
return 2;
}
}
self->request_rec->path_info = apr_pstrdup(self->request_rec->pool, path_info);
return 0;
}
/**
** request.build_wsgi_env(request self)
**
* Build a WSGI environment dictionary.
*
*/
/* these things never change and we never decref them */
static PyObject *wsgi_version = NULL;
static PyObject *wsgi_multithread = NULL;
static PyObject *wsgi_multiprocess = NULL;
static PyObject *req_build_wsgi_env(requestobject *self)
{
request_rec *r = self->request_rec;
apr_table_t *e = r->subprocess_env;
PyObject *env, *v;
const char *val;
int i, j;
env = PyDict_New();
if (!env)
return NULL;
int rc = set_wsgi_path_info(self);
if (rc == 1) {
/* bad base_uri, the error is already set */
Py_DECREF(env);
return NULL;
} else if (rc == 2) {
/* base_uri does not match uri, wsgi.py will decline */
Py_DECREF(env);
Py_INCREF(Py_None);
return Py_None;
}
/* this will create the correct SCRIPT_NAME based on our path_info now */
req_add_cgi_vars(self);
/* copy r->subprocess_env */
if (!self->subprocess_env)
self->subprocess_env = MpTable_FromTable(self->request_rec->subprocess_env);
else
((tableobject*)self->subprocess_env)->table = r->subprocess_env;
PyDict_Merge(env, (PyObject*)self->subprocess_env, 0);
/* authorization */
if ((val = apr_table_get(r->headers_in, "authorization"))) {
v = MpBytesOrUnicode_FromString(val);
PyDict_SetItemString(env, "HTTP_AUTHORIZATION", v);
Py_DECREF(v);
}
PyDict_SetItemString(env, "wsgi.input", (PyObject *) self);
PyDict_SetItemString(env, "wsgi.errors", PySys_GetObject("stderr"));
if (!wsgi_version) {
int result;
wsgi_version = Py_BuildValue("(ii)", 1, 0);
ap_mpm_query(AP_MPMQ_IS_THREADED, &result);
wsgi_multithread = PyBool_FromLong(result);
ap_mpm_query(AP_MPMQ_IS_FORKED, &result);
wsgi_multiprocess = PyBool_FromLong(result);
}
/* NOTE: these are global vars which we never decref! */
PyDict_SetItemString(env, "wsgi.version", wsgi_version);
PyDict_SetItemString(env, "wsgi.multithread", wsgi_multithread);
PyDict_SetItemString(env, "wsgi.multiprocess", wsgi_multiprocess);
val = apr_table_get(r->subprocess_env, "HTTPS");
if (!val || !strcasecmp(val, "off")) {
v = MpBytesOrUnicode_FromString("http");
PyDict_SetItemString(env, "wsgi.url_scheme", v);
Py_DECREF(v);
} else {
v = MpBytesOrUnicode_FromString("https");
PyDict_SetItemString(env, "wsgi.url_scheme", v);
Py_DECREF(v);
}
return env;
}
/**
** request.wsgi_start_response(self, args)
**
* The WSGI start_response()
*
*/
static PyObject *req_wsgi_start_response(requestobject *self, PyObject *args)
{
char *status_line = NULL;
PyObject *headers = NULL;
PyObject *exc_info = NULL;
int status, i;
if (! PyArg_ParseTuple(args, "sO|O:wsgi_start_response", &status_line, &headers, &exc_info))
return NULL;
if (!PyList_Check(headers)) {
PyErr_Format(PyExc_TypeError, "headers argument must be a list, not a '%.200s'",
headers->ob_type->tp_name);
return NULL;
}
/* I don't understand what PEP3333 wants us to do with the
* exception, we just re-raise it like other WSGI tools do */
if (exc_info) {
PyObject *exc, *value, *tb;
if (PyArg_UnpackTuple(exc_info, "wsgi_start_response", 3, 3, &exc, &value, &tb)) {
Py_INCREF(exc);
Py_INCREF(value);
Py_INCREF(tb);
PyErr_Restore(exc, value, tb);
}
return NULL;
}
/* add the headers */
for (i=0; i < PyList_Size(headers); i++) {
PyObject *key = NULL, *val = NULL;
char *k, *v;
PyObject *item = PyList_GetItem(headers, i);
if (!PyTuple_CheckExact(item)) {
PyErr_Format(PyExc_TypeError, "each header must be a tuple, not a '%.200s'",
item->ob_type->tp_name);
return NULL;
}
if (! PyArg_ParseTuple(item, "OO", &key, &val))
return NULL;
if (!((PyUnicode_CheckExact(key) || PyBytes_CheckExact(key)))) {
PyErr_Format(PyExc_TypeError, "header names must be strings, not '%.200s'",
key->ob_type->tp_name);
return NULL;
}
if (!((PyUnicode_CheckExact(val) || PyBytes_CheckExact(val)))) {
PyErr_Format(PyExc_TypeError, "header values must be strings, not '%.200s'",
val->ob_type->tp_name);
return NULL;
}
MP_ANYSTR_AS_STR(k, key, 1);
MP_ANYSTR_AS_STR(v, val, 1);
if ((!k) || (!v)) {
Py_DECREF(key); /* MP_ANYSTR_AS_STR */
Py_DECREF(val); /* MP_ANYSTR_AS_STR */
return NULL;
}
apr_table_add(self->request_rec->headers_out, k, v);
Py_DECREF(key); /* MP_ANYSTR_AS_STR */
Py_DECREF(val); /* MP_ANYSTR_AS_STR */
}
status = atoi(status_line);
if (!ap_is_HTTP_VALID_RESPONSE(status)) {
PyErr_SetString(PyExc_ValueError,
apr_psprintf(self->request_rec->pool,
"Invalid status line: %s", status_line));
return NULL;
}
self->request_rec->status_line = apr_pstrdup(self->request_rec->pool, status_line);
return PyObject_GetAttrString((PyObject*)self, "write");
}
/**
** valid_phase()
**
* utility func - makes sure a phase is valid
*/
static int valid_phase(const char *p)
{
if ((strcmp(p, "PythonHandler") != 0) &&
(strcmp(p, "PythonAuthenHandler") != 0) &&
(strcmp(p, "PythonPostReadRequestHandler") != 0) &&
(strcmp(p, "PythonTransHandler") != 0) &&
(strcmp(p, "PythonHeaderParserHandler") != 0) &&
(strcmp(p, "PythonAccessHandler") != 0) &&
(strcmp(p, "PythonAuthzHandler") != 0) &&
(strcmp(p, "PythonTypeHandler") != 0) &&
(strcmp(p, "PythonFixupHandler") != 0) &&
(strcmp(p, "PythonLogHandler") != 0) &&
(strcmp(p, "PythonInitHandler") != 0))
return 0;
else
return 1;
}
/**
** request.add_handler(request self, string phase, string handler)
**
* Allows to add another handler to the handler list.
*/
static PyObject *req_add_handler(requestobject *self, PyObject *args)
{
char *phase = NULL;
PyObject *o_phase;
char *handler;
const char *dir = NULL;
const char *currphase;
if (! PyArg_ParseTuple(args, "ss|z", &phase, &handler, &dir))
return NULL;
if (! valid_phase(phase)) {
PyErr_SetString(PyExc_IndexError,
apr_psprintf(self->request_rec->pool,
"Invalid phase: %s", phase));
return NULL;
}
/* Canonicalize path and add trailing slash at
* this point if directory was provided. */
if (dir) {
char *newpath = 0;
apr_status_t rv;
rv = apr_filepath_merge(&newpath, NULL, dir,
APR_FILEPATH_TRUENAME,
self->request_rec->pool);
/* If there is a failure, use the original path
* which was supplied. */
if (rv == APR_SUCCESS || rv == APR_EPATHWILD) {
dir = newpath;
if (dir[strlen(dir) - 1] != '/') {
dir = apr_pstrcat(self->request_rec->pool, dir, "/", NULL);
}
}
else {
/* dir is from Python, so duplicate it */
dir = apr_pstrdup(self->request_rec->pool, dir);
}
}
/* handler is from Python, so duplicate it */
handler = apr_pstrdup(self->request_rec->pool, handler);
/* which phase are we processing? */
o_phase = self->phase;
MP_ANYSTR_AS_STR(currphase, o_phase, 1);
/* are we in same phase as what's being added? */
if (strcmp(currphase, phase) == 0) {
/* then just append to hlist */
hlist_append(self->request_rec->pool, self->hlo->head,
handler, dir, 0, 0, NULL, NOTSILENT);
}
else {
/* this is a phase that we're not in */
py_req_config *req_config;
hl_entry *hle;
/* get request config */
req_config = (py_req_config *)
ap_get_module_config(self->request_rec->request_config,
&python_module);
hle = apr_hash_get(req_config->dynhls, phase, APR_HASH_KEY_STRING);
if (! hle) {
hle = hlist_new(self->request_rec->pool, handler, dir, 0, 0, NULL, NOTSILENT);
apr_hash_set(req_config->dynhls, phase, APR_HASH_KEY_STRING, hle);
}
else {
hlist_append(self->request_rec->pool, hle, handler, dir, 0, 0, NULL, NOTSILENT);
}
}
Py_XDECREF(o_phase);
Py_INCREF(Py_None);
return Py_None;
}
/**
** request.add_input_filter(request self, string name)
**
* Specifies that a pre registered filter be added to input filter chain.
*/
static PyObject *req_add_input_filter(requestobject *self, PyObject *args)
{
char *name;
py_req_config *req_config;
python_filter_ctx *ctx;
if (! PyArg_ParseTuple(args, "s", &name))
return NULL;
req_config = (py_req_config *) ap_get_module_config(
self->request_rec->request_config, &python_module);
if (apr_hash_get(req_config->in_filters, name, APR_HASH_KEY_STRING)) {
ctx = (python_filter_ctx *) apr_pcalloc(self->request_rec->pool,
sizeof(python_filter_ctx));
ctx->name = apr_pstrdup(self->request_rec->pool, name);
ap_add_input_filter(FILTER_NAME, ctx, self->request_rec,
self->request_rec->connection);
} else {
ap_add_input_filter(name, NULL, self->request_rec,
self->request_rec->connection);
}
Py_INCREF(Py_None);
return Py_None;
}
/**
** request.add_output_filter(request self, string name)
**
* Specifies that a pre registered filter be added to output filter chain.
*/
static PyObject *req_add_output_filter(requestobject *self, PyObject *args)
{
char *name;
py_req_config *req_config;
python_filter_ctx *ctx;
if (! PyArg_ParseTuple(args, "s", &name))
return NULL;
req_config = (py_req_config *) ap_get_module_config(
self->request_rec->request_config, &python_module);
if (apr_hash_get(req_config->out_filters, name, APR_HASH_KEY_STRING)) {
ctx = (python_filter_ctx *) apr_pcalloc(self->request_rec->pool,
sizeof(python_filter_ctx));
ctx->name = apr_pstrdup(self->request_rec->pool, name);
ap_add_output_filter(FILTER_NAME, ctx, self->request_rec,
self->request_rec->connection);
} else {
ap_add_output_filter(name, NULL, self->request_rec,
self->request_rec->connection);
}
Py_INCREF(Py_None);
return Py_None;
}
/**
** request.register_input_filter(request self, string name, string handler, list dir)
**
* Registers an input filter active for life of the request.
*/
static PyObject *req_register_input_filter(requestobject *self, PyObject *args)
{
char *name;
char *handler;
char *dir = NULL;
py_req_config *req_config;
py_handler *fh;
if (! PyArg_ParseTuple(args, "ss|s", &name, &handler, &dir))
return NULL;
req_config = (py_req_config *) ap_get_module_config(
self->request_rec->request_config, &python_module);
fh = (py_handler *) apr_pcalloc(self->request_rec->pool,
sizeof(py_handler));
fh->handler = apr_pstrdup(self->request_rec->pool, handler);
/* Canonicalize path and add trailing slash at
* this point if directory was provided. */
if (dir) {
char *newpath = 0;
apr_status_t rv;
rv = apr_filepath_merge(&newpath, NULL, dir,
APR_FILEPATH_TRUENAME,
self->request_rec->pool);
/* If there is a failure, use the original path
* which was supplied. */
if (rv == APR_SUCCESS || rv == APR_EPATHWILD) {
dir = newpath;
if (dir[strlen(dir) - 1] != '/') {
dir = apr_pstrcat(self->request_rec->pool, dir, "/", NULL);
}
fh->directory = dir;
} else {
fh->directory = apr_pstrdup(self->request_rec->pool, dir);
}
}
apr_hash_set(req_config->in_filters,
apr_pstrdup(self->request_rec->pool, name),
APR_HASH_KEY_STRING, fh);
Py_INCREF(Py_None);
return Py_None;
}
/**
** request.register_output_filter(request self, string name, string handler, list dir)
**
* Registers an output filter active for life of the request.
*/
static PyObject *req_register_output_filter(requestobject *self, PyObject *args)
{
char *name;
char *handler;
char *dir = NULL;
py_req_config *req_config;
py_handler *fh;
if (! PyArg_ParseTuple(args, "ss|s", &name, &handler, &dir))
return NULL;
req_config = (py_req_config *) ap_get_module_config(
self->request_rec->request_config, &python_module);
fh = (py_handler *) apr_pcalloc(self->request_rec->pool,
sizeof(py_handler));
fh->handler = apr_pstrdup(self->request_rec->pool, handler);
/* Canonicalize path and add trailing slash at
* this point if directory was provided. */
if (dir) {
char *newpath = 0;
apr_status_t rv;
rv = apr_filepath_merge(&newpath, NULL, dir,
APR_FILEPATH_TRUENAME,
self->request_rec->pool);
/* If there is a failure, use the original path
* which was supplied. */
if (rv == APR_SUCCESS || rv == APR_EPATHWILD) {
dir = newpath;
if (dir[strlen(dir) - 1] != '/') {
dir = apr_pstrcat(self->request_rec->pool, dir, "/", NULL);
}
fh->directory = dir;
} else {
fh->directory = apr_pstrdup(self->request_rec->pool, dir);
}
}
apr_hash_set(req_config->out_filters,
apr_pstrdup(self->request_rec->pool, name),
APR_HASH_KEY_STRING, fh);
Py_INCREF(Py_None);
return Py_None;
}
/**
** request.allow_methods(request self, list methods, reset=0)
**
* a wrapper around ap_allow_methods. (used for the "allow:" header
* to be passed to client when needed.)
*/
static PyObject *req_allow_methods(requestobject *self, PyObject *args)
{
PyObject *methods;
int reset = 0;
int len, i;
if (! PyArg_ParseTuple(args, "O|i", &methods, &reset))
return NULL;
if (! PySequence_Check(methods)){
PyErr_SetString(PyExc_TypeError,
"First argument must be a sequence");
return NULL;
}
len = PySequence_Length(methods);
if (len) {
PyObject *method;
char *m;
method = PySequence_GetItem(methods, 0);
MP_ANYSTR_AS_STR(m, method, 1);
if (!m) {
Py_DECREF(method); /* MP_ANYSTR_AS_STR */
return NULL;
}
ap_allow_methods(self->request_rec, (reset == REPLACE_ALLOW), m, NULL);
Py_DECREF(method); /* MP_ANYSTR_AS_STR */
for (i = 1; i < len; i++) {
method = PySequence_GetItem(methods, i);
MP_ANYSTR_AS_STR(m, method, 1);
if (!m) {
Py_DECREF(method); /* MP_ANYSTR_AS_STR */
return NULL;
}
ap_allow_methods(self->request_rec, MERGE_ALLOW, m, NULL);
Py_DECREF(method); /* MP_ANYSTR_AS_STR */
}
}
Py_INCREF(Py_None);
return Py_None;
}
/**
** request.is_https(self)
**
* mod_ssl ssl_is_https() wrapper
*/
static PyObject * req_is_https(requestobject *self)
{
int is_https;
if (!optfn_is_https)
optfn_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
is_https = optfn_is_https && optfn_is_https(self->request_rec->connection);
return PyLong_FromLong(is_https);
}
/**
** request.ssl_var_lookup(self, string variable_name)
**
* mod_ssl ssl_var_lookup() wrapper
*/
static PyObject * req_ssl_var_lookup(requestobject *self, PyObject *args)
{
char *var_name;
if (! PyArg_ParseTuple(args, "s", &var_name))
return NULL; /* error */
if (!optfn_ssl_var_lookup)
optfn_ssl_var_lookup = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
if (optfn_ssl_var_lookup) {
const char *val;
val = optfn_ssl_var_lookup(self->request_rec->pool,
self->request_rec->server,
self->request_rec->connection,
self->request_rec,
var_name);
if (val)
return MpBytesOrUnicode_FromString(val);
}
/* variable not found, or mod_ssl is not loaded */
Py_INCREF(Py_None);
return Py_None;
}
/**
** request.document_root(self)
**
* ap_docuement_root wrapper
*/
static PyObject *req_document_root(requestobject *self)
{
return MpBytesOrUnicode_FromString(ap_document_root(self->request_rec));
}
/**
** request.get_basic_auth_pw(request self)
**
* get basic authentication password,
* similar to ap_get_basic_auth_pw
*/
static PyObject * req_get_basic_auth_pw(requestobject *self, PyObject *args)
{
const char *pw;
request_rec *req;
/* http://stackoverflow.com/questions/702629/utf-8-characters-mangled-in-http-basic-auth-username/703341#703341 */
/* Latin1 is Safari, Chrome and Mozilla - otherwise it can be decoded manually */
req = self->request_rec;
if (! ap_get_basic_auth_pw(req, &pw)) {
#if PY_MAJOR_VERSION < 3
return PyBytes_FromString(pw);
#else
return PyUnicode_DecodeLatin1(pw, strlen(pw), NULL);
#endif
} else {
Py_INCREF(Py_None);
return Py_None;
}
}
/**
** request.auth_name(self)
**
* ap_auth_name wrapper
*/
static PyObject *req_auth_name(requestobject *self)
{
const char *auth_name = ap_auth_name(self->request_rec);
if (!auth_name) {
Py_INCREF(Py_None);
return Py_None;
}
return MpBytesOrUnicode_FromString(auth_name);
}
/**
** request.auth_type(self)
**
* ap_auth_type wrapper
*/
static PyObject *req_auth_type(requestobject *self)
{
const char *auth_type = ap_auth_type(self->request_rec);
if (!auth_type) {
Py_INCREF(Py_None);
return Py_None;
}
return MpBytesOrUnicode_FromString(auth_type);
}
/**
** request.construct_url(self)
**
* ap_construct_url wrapper
*/
static PyObject *req_construct_url(requestobject *self, PyObject *args)
{
char *uri;
if (! PyArg_ParseTuple(args, "s", &uri))
return NULL;
return MpBytesOrUnicode_FromString(ap_construct_url(self->request_rec->pool,
uri, self->request_rec));
}
/**
** request.discard_request_body(request self)
**
* discard content supplied with request
*/
static PyObject * req_discard_request_body(requestobject *self)
{
return PyLong_FromLong(ap_discard_request_body(self->request_rec));
}
/**
** request.get_addhandler_exts(request self)
**
* Returns file extentions that were given as argument to AddHandler mod_mime
* directive, if any, if at all. This is useful for the Publisher, which can
* chop off file extentions for modules based on this info.
*
* XXX Due to the way this is implemented, it is best stay undocumented.
*/
static PyObject * req_get_addhandler_exts(requestobject *self, PyObject *args)
{
char *exts = get_addhandler_extensions(self->request_rec);
if (exts)
return MpBytesOrUnicode_FromString(exts);
else
return MpBytesOrUnicode_FromString("");
}
/**
** request.get_config(request self)
**
* Returns the config directives set through Python* apache directives.
* except for Python*Handler and PythonOption (which you get via get_options).
*/
static PyObject * req_get_config(requestobject *self)
{
py_config *conf =
(py_config *) ap_get_module_config(self->request_rec->per_dir_config,
&python_module);
if (!self->config)
self->config = MpTable_FromTable(conf->directives);
if (((tableobject*)self->config)->table != conf->directives)
((tableobject*)self->config)->table = conf->directives;
Py_INCREF(self->config);
return self->config;
}
/**
** request.get_remodte_host(request self, [int type])
**
* An interface to the ap_get_remote_host function.
*/
static PyObject * req_get_remote_host(requestobject *self, PyObject *args)
{
int type = REMOTE_NAME;
PyObject *str_is_ip = Py_None;
int _str_is_ip;
const char *host;
if (! PyArg_ParseTuple(args, "|iO", &type, &str_is_ip))
return NULL;
if (str_is_ip != Py_None) {
host = ap_get_remote_host(self->request_rec->connection,
self->request_rec->per_dir_config, type, &_str_is_ip);
}
else {
host = ap_get_remote_host(self->request_rec->connection,
self->request_rec->per_dir_config, type, NULL);
}
if (! host) {
Py_INCREF(Py_None);
return Py_None;
}
else {
if (str_is_ip != Py_None) {
return Py_BuildValue("(s,i)", host, _str_is_ip);
}
else {
return MpBytesOrUnicode_FromString(host);
}
}
}
/**
** request.get_options(request self)
**
*/
static PyObject * req_get_options(requestobject *self, PyObject *args)
{
py_config *conf =
(py_config *) ap_get_module_config(self->request_rec->per_dir_config,
&python_module);
if (!self->options)
self->options = MpTable_FromTable(conf->options);
if (((tableobject*)self->options)->table != conf->options)
((tableobject*)self->options)->table = conf->options;
const apr_array_header_t* ah = apr_table_elts(conf->options);
apr_table_entry_t* elts = (apr_table_entry_t *) ah->elts;
int i;
/* Remove the empty values as a way to unset values.