forked from MITRECND/htpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htpy.c
1234 lines (1077 loc) · 42.2 KB
/
htpy.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
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) 2013 The MITRE Corporation. All rights reserved.
* Copyright (c) 2023 Wesley Shields. 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 AUTHOR 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 AUTHOR 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.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include "../htp_config_auto_gen.h"
#include "htp.h"
#include "htp_private.h"
#define HTPY_VERSION "0.30"
static PyObject *htpy_error;
static PyObject *htpy_stop;
/*
* We set the python connection parser as user_data in the libhtp connection
* parser. Most callbacks are given a way to eventually get to the connection
* parser by doing something like this:
*
* PyObject *obj = (PyObject *) htp_connp_get_user_data(tx->connp);
*
* We store the python objects for the callbacks in the connection parser
* object. Once we have the connection parser using the above snippet
* we can call the appropriate python function by using obj->foo_callback.
*
* The only callback that is not able to get to the connection parser is
* the request_file_data callback. Since we can't get to a connection
* parser from there we are storing the python object as a global.
*/
PyObject *request_file_data_callback;
typedef struct {
PyObject_HEAD
htp_cfg_t *cfg;
} htpy_config;
static PyObject *htpy_config_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
htpy_config *self;
self = (htpy_config *) type->tp_alloc(type, 0);
return (PyObject *) self;
}
static int htpy_config_init(htpy_config *self, PyObject *args, PyObject *kwds) {
self->cfg = htp_config_create();
if (!self->cfg)
return -1;
htp_config_set_tx_auto_destroy(self->cfg, 1);
return 0;
}
static void htpy_config_dealloc(htpy_config *self) {
htp_config_destroy(self->cfg);
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyMethodDef htpy_config_methods[] = {
{ NULL }
};
#define CONFIG_GET(ATTR) \
static PyObject *htpy_config_get_##ATTR(htpy_config *self, void *closure) { \
PyObject *ret; \
ret = Py_BuildValue("i", self->cfg->ATTR); \
if (!ret) { \
PyErr_SetString(htpy_error, "Unable to get this attribute."); \
return NULL; \
} \
return ret; \
}
CONFIG_GET(log_level)
CONFIG_GET(tx_auto_destroy)
/**
* To get the response decompression setting there is an int
* in the config called "response_decompression_enabled". Unfortunately
* this doesn't fit with the above macro and so instead the following
* function takes care of getting the value.
*/
static PyObject *htpy_config_get_response_decompression(htpy_config *self, void *closure) {
PyObject *ret;
ret = Py_BuildValue("i", self->cfg->response_decompression_enabled);
if (!ret) {
PyErr_SetString(htpy_error, "Unable to get this attribute.");
return NULL;
}
return ret;
}
#define CONFIG_SET(ATTR) \
static int htpy_config_set_##ATTR(htpy_config *self, PyObject *value, void *closure) { \
int v; \
if (!value) { \
PyErr_SetString(htpy_error, "Value may not be None."); \
return -1; \
} \
if (!PyLong_Check(value)) { \
PyErr_SetString(htpy_error, "Attribute must be of type int."); \
return -1; \
} \
v = (int) PyLong_AsLong(value); \
htp_config_set_##ATTR((htp_cfg_t *) self->cfg, v); \
return 0; \
}
CONFIG_SET(tx_auto_destroy)
CONFIG_SET(response_decompression)
/*
* Sadly the log level is not exposed like others. Only way to set it
* is to manually set it in the config structure directly.
*
* I'm not checking for values above the maximum log level. If someone
* wants to do something stupid like set it to htpy.HTP_1_1 (101) then
* they can have fun dealing with it.
*/
static int htpy_config_set_log_level(htpy_config *self, PyObject *value, void *closure) {
int v;
if (!value) {
PyErr_SetString(htpy_error, "Value may not be None.");
return -1;
}
if (!PyLong_Check(value)) {
PyErr_SetString(htpy_error, "Attribute must be of type int.");
return -1;
}
v = (int) PyLong_AsLong(value);
self->cfg->log_level = v;
return 0;
}
static PyGetSetDef htpy_config_getseters[] = {
{"log_level",
(getter) htpy_config_get_log_level,
(setter) htpy_config_set_log_level,
"Logs with a level less than this will be ignored.", NULL},
{"tx_auto_destroy",
(getter) htpy_config_get_tx_auto_destroy,
(setter) htpy_config_set_tx_auto_destroy,
"Automatically destroy transactions", NULL},
{"response_decompression",
(getter) htpy_config_get_response_decompression,
(setter) htpy_config_set_response_decompression,
"Enable response decompression", NULL},
{NULL}
};
static PyMemberDef htpy_config_members[] = {
{ NULL }
};
static PyTypeObject htpy_config_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"htpy.config", /* tp_name */
sizeof(htpy_config), /* tp_basicsize */
0, /* tp_itemsize */
(destructor) htpy_config_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"config object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
htpy_config_methods, /* tp_methods */
htpy_config_members, /* tp_members */
htpy_config_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc) htpy_config_init, /* tp_init */
0, /* tp_alloc */
htpy_config_new, /* tp_new */
};
typedef struct {
PyObject_HEAD
htp_connp_t *connp;
PyObject *cfg;
PyObject *obj_store;
/* Callbacks */
PyObject *request_start_callback;
PyObject *request_line_callback;
PyObject *request_uri_normalize_callback;
PyObject *request_headers_callback;
PyObject *request_header_data_callback;
PyObject *request_body_data_callback;
PyObject *request_trailer_callback;
PyObject *request_trailer_data_callback;
PyObject *request_complete_callback;
PyObject *response_start_callback;
PyObject *response_line_callback;
PyObject *response_headers_callback;
PyObject *response_header_data_callback;
PyObject *response_body_data_callback;
PyObject *response_trailer_callback;
PyObject *response_trailer_data_callback;
PyObject *response_complete_callback;
PyObject *transaction_complete_callback;
PyObject *log_callback;
} htpy_connp;
static PyObject *htpy_connp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
htpy_connp *self;
self = (htpy_connp *) type->tp_alloc(type, 0);
return (PyObject *) self;
}
static void htpy_connp_dealloc(htpy_connp *self) {
// Decrement reference counters and free the underlying libhtp backed storage.
Py_XDECREF(self->obj_store);
Py_XDECREF(self->cfg);
Py_XDECREF(self->request_start_callback);
Py_XDECREF(self->request_line_callback);
Py_XDECREF(self->request_uri_normalize_callback);
Py_XDECREF(self->request_headers_callback);
Py_XDECREF(self->request_header_data_callback);
Py_XDECREF(self->request_body_data_callback);
Py_XDECREF(self->request_trailer_callback);
Py_XDECREF(self->request_trailer_data_callback);
Py_XDECREF(self->request_complete_callback);
Py_XDECREF(self->response_start_callback);
Py_XDECREF(self->response_line_callback);
Py_XDECREF(self->response_headers_callback);
Py_XDECREF(self->response_header_data_callback);
Py_XDECREF(self->response_body_data_callback);
Py_XDECREF(self->response_trailer_callback);
Py_XDECREF(self->response_trailer_data_callback);
Py_XDECREF(self->response_complete_callback);
Py_XDECREF(self->transaction_complete_callback);
Py_XDECREF(self->log_callback);
htp_connp_destroy_all(self->connp);
Py_TYPE(self)->tp_free((PyObject *) self);
}
static int htpy_connp_init(htpy_connp *self, PyObject *args, PyObject *kwds) {
PyObject *cfg_obj = NULL;
if (!PyArg_ParseTuple(args, "|O:htpy_connp_init", &cfg_obj))
return -1;
/*
* If we are not given a config object as an argument create an
* htp_cfg_t and use that.
*/
if (!cfg_obj)
cfg_obj = PyObject_CallObject((PyObject *) &htpy_config_type, NULL);
else
Py_XINCREF(cfg_obj);
if (!cfg_obj) {
PyErr_SetString(htpy_error, "Unable to create config object.");
return -1;
}
self->cfg = cfg_obj;
self->connp = htp_connp_create(((htpy_config *) cfg_obj)->cfg);
if (!self->connp) {
PyErr_SetString(htpy_error, "Unable to create connection parser.");
return -1;
}
htp_connp_set_user_data(self->connp, (void *) self);
return 0;
}
/*
* Callback handlers.
*
* Libhtp will call one of these callbacks. This callback will then get the
* connp, convert it into a PyObject and pass that to the real callback.
* It will then convert the returned PyObject to an int and pass that back to
* libhtp.
*
* The callbacks that take a htp_tx_t are defined with CALLBACK_TX. The
* log callback is not defined in a macro because there is only one of it's
* type.
*
* XXX: Add support for removing callbacks?
*/
#define CALLBACK(CB) \
int htpy_##CB##_callback(htp_tx_t *tx) { \
PyObject *obj = (PyObject *) htp_connp_get_user_data(tx->connp); \
PyObject *arglist; \
PyObject *res; \
long i; \
if (((htpy_connp *) obj)->obj_store) \
arglist = Py_BuildValue("(OO)", obj, ((htpy_connp *) obj)->obj_store); \
else \
arglist = Py_BuildValue("(O)", obj); \
if (!arglist) \
return HTP_ERROR; \
res = PyObject_CallObject(((htpy_connp *) obj)->CB##_callback, arglist); \
Py_DECREF(arglist); \
if (PyErr_Occurred() != NULL) { \
PyErr_PrintEx(0); \
return HTP_ERROR; \
} \
i = PyLong_AsLong(res); \
Py_DECREF(res); \
return (int) i; \
}
CALLBACK(request_start)
CALLBACK(request_line)
CALLBACK(request_uri_normalize)
CALLBACK(request_headers)
CALLBACK(request_trailer)
CALLBACK(request_complete)
CALLBACK(response_start)
CALLBACK(response_line)
CALLBACK(response_headers)
CALLBACK(response_trailer)
CALLBACK(response_complete)
CALLBACK(transaction_complete)
/* These callbacks take a htp_tx_data_t pointer. */
#define CALLBACK_TX(CB) \
int htpy_##CB##_callback(htp_tx_data_t *txd) { \
PyObject *obj = (PyObject *) htp_connp_get_user_data(txd->tx->connp); \
PyObject *arglist; \
PyObject *res; \
long i; \
if (((htpy_connp *) obj)->obj_store) \
arglist = Py_BuildValue("(s#IO)", txd->data, txd->len, txd->len, ((htpy_connp *) obj)->obj_store); \
else \
arglist = Py_BuildValue("(s#I)", txd->data, txd->len, txd->len); \
if (!arglist) \
return HTP_ERROR; \
res = PyObject_CallObject(((htpy_connp *) obj)->CB##_callback, arglist); \
Py_DECREF(arglist); \
if (PyErr_Occurred() != NULL) { \
PyErr_PrintEx(0); \
return HTP_ERROR; \
} \
i = PyLong_AsLong(res); \
Py_DECREF(res); \
return (int) i; \
}
CALLBACK_TX(request_header_data)
CALLBACK_TX(request_body_data)
CALLBACK_TX(request_trailer_data)
CALLBACK_TX(response_header_data)
CALLBACK_TX(response_body_data)
CALLBACK_TX(response_trailer_data)
/* Another special case callback. This one takes a htp_file_data_t pointer. */
int htpy_request_file_data_callback(htp_file_data_t *file_data) {
long i;
PyObject *res;
PyObject *arglist;
PyObject *data_key, *data_val;
PyObject *filename_key, *filename_val;
PyObject *tmpname_key, *tmpname_val;
PyObject *dict = PyDict_New();
if (!dict) {
PyErr_SetString(htpy_error, "Unable to create dictionary.");
return HTP_ERROR;
}
data_key = Py_BuildValue("s", "data");
data_val = Py_BuildValue("s#", file_data->data, file_data->len);
if (!data_key || !data_val) {
Py_DECREF(dict);
return HTP_ERROR;
}
if (PyDict_SetItem(dict, data_key, data_val) == -1) {
Py_DECREF(dict);
return HTP_ERROR;
}
if (file_data->file->filename) {
filename_key = Py_BuildValue("s", "filename");
filename_val = Py_BuildValue("s#", bstr_ptr(file_data->file->filename), bstr_len(file_data->file->filename));
if (PyDict_SetItem(dict, filename_key, filename_val) == -1) {
Py_DECREF(dict);
return HTP_ERROR;
}
}
if (file_data->file->tmpname) {
tmpname_key = Py_BuildValue("s", "tmpname");
tmpname_val = Py_BuildValue("s", file_data->file->tmpname);
if (PyDict_SetItem(dict, tmpname_key, tmpname_val) == -1) {
Py_DECREF(dict);
return HTP_ERROR;
}
}
arglist = Py_BuildValue("(O)", dict);
if (!arglist)
return HTP_ERROR;
res = PyObject_CallObject(request_file_data_callback, arglist);
Py_DECREF(arglist);
if (PyErr_Occurred() != NULL) {
PyErr_PrintEx(0);
return HTP_ERROR;
}
i = PyLong_AsLong(res);
Py_DECREF(res);
return (int) i;
}
int htpy_log_callback(htp_log_t *log) {
PyObject *obj = (PyObject *) htp_connp_get_user_data(log->connp);
PyObject *arglist = NULL;
PyObject *res;
long i;
if (((htpy_connp *) obj)->obj_store)
arglist = Py_BuildValue("(OsiO)", (htpy_connp *) obj, log->msg, log->level, ((htpy_connp *) obj)->obj_store);
else
arglist = Py_BuildValue("(Osi)", (htpy_connp *) obj, log->msg, log->level);
if (!arglist)
return HTP_ERROR;
res = PyObject_CallObject(((htpy_connp *) obj)->log_callback, arglist);
Py_DECREF(arglist);
if (PyErr_Occurred() != NULL) {
PyErr_PrintEx(0);
return HTP_ERROR;
}
i = PyLong_AsLong(res);
Py_DECREF(res);
return (int) i;
}
/* Registering callbacks... */
#define REGISTER_CALLBACK(CB) \
static PyObject *htpy_connp_register_##CB(PyObject *self, PyObject *args) { \
PyObject *res = NULL; \
PyObject *temp; \
if (PyArg_ParseTuple(args, "O:htpy_connp_register_##CB", &temp)) { \
if (!PyCallable_Check(temp)) { \
PyErr_SetString(PyExc_TypeError, "parameter must be callable"); \
return NULL; \
} \
Py_XINCREF(temp); \
Py_XDECREF(((htpy_connp *) self)->CB##_callback); \
((htpy_connp *) self)->CB##_callback = temp; \
htp_config_register_##CB(((htpy_connp *) self)->connp->cfg, htpy_##CB##_callback); \
Py_INCREF(Py_None); \
res = Py_None; \
} \
return res; \
}
REGISTER_CALLBACK(request_start)
REGISTER_CALLBACK(request_line)
REGISTER_CALLBACK(request_uri_normalize)
REGISTER_CALLBACK(request_headers)
REGISTER_CALLBACK(request_header_data)
REGISTER_CALLBACK(request_body_data)
REGISTER_CALLBACK(request_trailer)
REGISTER_CALLBACK(request_trailer_data)
REGISTER_CALLBACK(request_complete)
REGISTER_CALLBACK(response_start)
REGISTER_CALLBACK(response_line)
REGISTER_CALLBACK(response_headers)
REGISTER_CALLBACK(response_header_data)
REGISTER_CALLBACK(response_body_data)
REGISTER_CALLBACK(response_trailer)
REGISTER_CALLBACK(response_trailer_data)
REGISTER_CALLBACK(response_complete)
REGISTER_CALLBACK(transaction_complete)
REGISTER_CALLBACK(log)
static PyObject *htpy_connp_register_request_file_data(PyObject *self, PyObject *args) {
PyObject *res = NULL;
PyObject *temp;
int extract = 0;
if (PyArg_ParseTuple(args, "O|i:htpy_connp_register_request_file_data", &temp, &extract)) {
if (!PyCallable_Check(temp)) {
PyErr_SetString(PyExc_TypeError, "parameter must be callable");
return NULL;
}
Py_XINCREF(temp);
Py_XDECREF(request_file_data_callback);
request_file_data_callback = temp;
if (extract)
((htpy_connp *) self)->connp->cfg->extract_request_files = 1;
htp_config_register_multipart_parser(((htpy_connp *) self)->connp->cfg);
htp_config_register_request_file_data(((htpy_connp *) self)->connp->cfg, htpy_request_file_data_callback);
Py_INCREF(Py_None);
res = Py_None;
}
return res;
}
/* Return a header who'se key is the given string. */
#define GET_HEADER(TYPE) \
static PyObject *htpy_connp_get_##TYPE##_header(PyObject *self, PyObject *args) { \
PyObject *ret; \
htp_header_t *hdr; \
htp_tx_t *tx = NULL; \
const char *p = NULL; \
if (!PyArg_ParseTuple(args, "s:htpy_connp_get_##TYPE##_header", &p)) \
return NULL; \
tx = htp_list_get(((htpy_connp *) self)->connp->conn->transactions, htp_list_size(((htpy_connp *) self)->connp->conn->transactions) - 1); \
if (!tx || !tx->TYPE##_headers) { \
PyErr_SetString(htpy_error, "Missing transaction or headers."); \
return NULL; \
} \
hdr = htp_table_get_c(tx->TYPE##_headers, p); \
if (!hdr) \
Py_RETURN_NONE; \
ret = Py_BuildValue("s#", bstr_ptr(hdr->value), bstr_len(hdr->value)); \
if (!ret) \
return NULL; \
return ret; \
}
GET_HEADER(request)
GET_HEADER(response)
/* Return a dictionary of all request or response headers. */
#define GET_ALL_HEADERS(TYPE) \
static PyObject *htpy_connp_get_all_##TYPE##_headers(PyObject *self, PyObject *args) { \
size_t i; \
size_t n; \
htp_header_t *hdr = NULL; \
PyObject *key, *val; \
htp_tx_t *tx = NULL; \
PyObject *ret = PyDict_New(); \
if (!ret) { \
PyErr_SetString(htpy_error, "Unable to create return dictionary."); \
return NULL; \
} \
tx = htp_list_get(((htpy_connp *) self)->connp->conn->transactions, htp_list_size(((htpy_connp *) self)->connp->conn->transactions) - 1); \
if (!tx || !tx->TYPE##_headers) { \
PyErr_SetString(htpy_error, "Missing transaction or headers."); \
Py_DECREF(ret); \
return NULL; \
} \
for (i = 0, n = htp_table_size(tx->TYPE##_headers); i < n; i++) { \
hdr = htp_table_get_index(tx->TYPE##_headers, i, NULL); \
key = Py_BuildValue("s#", bstr_ptr(hdr->name), bstr_len(hdr->name)); \
val = Py_BuildValue("s#", bstr_ptr(hdr->value), bstr_len(hdr->value)); \
if (!key || !val) { \
Py_DECREF(ret); \
Py_XDECREF(key); \
Py_XDECREF(val); \
return NULL; \
} \
if (PyDict_SetItem(ret, key, val) == -1) { \
Py_DECREF(ret); \
return NULL; \
} \
Py_XDECREF(key); \
Py_XDECREF(val); \
} \
return ret; \
}
GET_ALL_HEADERS(request)
GET_ALL_HEADERS(response)
/*
* XXX: Not sure I like mucking around in the transaction to get the method,
* but I'm not sure of a better way.
*/
static PyObject *htpy_connp_get_method(PyObject *self, PyObject *args) {
htp_tx_t *tx = NULL;
tx = htp_list_get(((htpy_connp *) self)->connp->conn->transactions, htp_list_size(((htpy_connp *) self)->connp->conn->transactions) - 1);
if (!tx || !tx->request_method) {
PyErr_SetString(htpy_error, "Missing transaction or request method.");
return NULL;
}
return Py_BuildValue("s#", bstr_ptr(tx->request_method), bstr_len(tx->request_method));
}
static PyObject *htpy_connp_set_obj(PyObject *self, PyObject *args) {
PyObject *obj;
if (!PyArg_ParseTuple(args, "O:htpy_connp_set_obj", &obj))
return NULL;
/*
* Remove a reference to any existing object. This ensures we do not leak
* objects in the case of someone calling this multiple times.
*/
Py_XDECREF(((htpy_connp *) self)->obj_store);
Py_XINCREF(obj);
((htpy_connp *) self)->obj_store = obj;
Py_RETURN_NONE;
}
static PyObject *htpy_connp_del_obj(PyObject *self, PyObject *args) {
Py_XDECREF(((htpy_connp *) self)->obj_store);
((htpy_connp *) self)->obj_store = NULL;
Py_RETURN_NONE;
}
/*
* XXX: Not sure I like mucking around in the transaction to get the status,
* but I'm not sure of a better way.
*/
static PyObject *htpy_connp_get_response_status_string(PyObject *self, PyObject *args) {
PyObject *ret;
htp_tx_t *tx = NULL;
tx = htp_list_get(((htpy_connp *) self)->connp->conn->transactions, htp_list_size(((htpy_connp *) self)->connp->conn->transactions) - 1);
if (!tx) {
PyErr_SetString(htpy_error, "Missing transaction.");
return NULL;
}
ret = Py_BuildValue("s#", bstr_ptr(tx->response_status), bstr_len(tx->response_status));
return ret;
}
static PyObject *htpy_connp_get_response_status(PyObject *self, PyObject *args) {
htp_tx_t *tx = NULL;
tx = htp_list_get(((htpy_connp *) self)->connp->conn->transactions, htp_list_size(((htpy_connp *) self)->connp->conn->transactions) - 1);
if (!tx) {
PyErr_SetString(htpy_error, "Missing transaction.");
return NULL;
}
return Py_BuildValue("i", tx->response_status_number);
}
static PyObject *htpy_connp_get_response_line(PyObject *self, PyObject *args) {
if (!((htpy_connp *) self)->connp->out_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->out_tx->response_line)
Py_RETURN_NONE;
return Py_BuildValue("s#", bstr_ptr(((htpy_connp *) self)->connp->out_tx->response_line), bstr_len(((htpy_connp *) self)->connp->out_tx->response_line));
}
static PyObject *htpy_connp_get_request_line(PyObject *self, PyObject *args) {
if (!((htpy_connp *) self)->connp->in_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->in_tx->request_line)
Py_RETURN_NONE;
return Py_BuildValue("s#", bstr_ptr(((htpy_connp *) self)->connp->in_tx->request_line), bstr_len(((htpy_connp *) self)->connp->in_tx->request_line));
}
/* See HTTP 1.1 RFC 4.3 Message Body */
/*
* The length of the response message-body. In most cases, this value will be
* the same as response_entity_len. The values will be different if response
* compression or chunking were applied. In that case, response_message_len
* contains the length of the response body as it has been seen over TCP;
* response_entity_len contains the length after de-chunking and decompression.
*/
static PyObject *htpy_connp_get_response_message_length(PyObject *self, PyObject *args) {
if (!((htpy_connp *) self)->connp->out_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->out_tx->response_message_len)
Py_RETURN_NONE;
return Py_BuildValue("i", ((htpy_connp *) self)->connp->out_tx->response_message_len);
}
/*
* The length of the request message-body. In most cases, this value will be the
* same as request_entity_len. The values will be different if request
* compression or chunking were applied. In that case, request_message_len
* contains the length of the request body as it has been seen over TCP;
* request_entity_len contains length after de-chunking and decompression.
*/
static PyObject *htpy_connp_get_request_message_length(PyObject *self, PyObject *args) {
if (!((htpy_connp *) self)->connp->in_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->in_tx->request_message_len)
Py_RETURN_NONE;
return Py_BuildValue("i", ((htpy_connp *) self)->connp->in_tx->request_message_len);
}
/*
* The length of the response entity-body. In most cases, this value will be the
* same as response_message_len. The values will be different if request
* compression or chunking were applied. In that case, response_message_len
* contains the length of the response body as it has been seen over TCP;
* response_entity_len contains length after de-chunking and decompression.
*/
static PyObject *htpy_connp_get_response_entity_length(PyObject *self, PyObject *args) {
PyObject *ret;
if (!((htpy_connp *) self)->connp->out_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->out_tx->response_entity_len)
Py_RETURN_NONE;
ret = Py_BuildValue("i", ((htpy_connp *) self)->connp->out_tx->response_entity_len);
return ret;
}
/*
* The length of the request entity-body. In most cases, this value will be the
* same as request_message_len. The values will be different if request
* compression or chunking were applied. In that case, request_message_len
* contains the length of the request body as it has been seen over TCP;
* request_entity_len contains length after de-chunking and decompression.
*/
static PyObject *htpy_connp_get_request_entity_length(PyObject *self, PyObject *args) {
if (!((htpy_connp *) self)->connp->in_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->in_tx->request_entity_len)
Py_RETURN_NONE;
return Py_BuildValue("i", ((htpy_connp *) self)->connp->in_tx->request_entity_len);
}
/* These do the actual parsing. */
#define DATA(TYPE) \
static PyObject *htpy_connp_##TYPE##_data(PyObject *self, PyObject *args) { \
const char *data; \
PyObject *ret; \
int x; \
Py_ssize_t len; \
if (!PyArg_ParseTuple(args, "s#:htpy_connp_##TYPE##_data", &data, &len)) \
return NULL; \
x = htp_connp_##TYPE##_data(((htpy_connp *) self)->connp, NULL, (unsigned char *) data, len); \
if (x == HTP_STREAM_ERROR) { \
PyErr_SetString(htpy_error, "Stream error."); \
return NULL; \
} \
if (x == HTP_STREAM_STOP) { \
PyErr_SetString(htpy_stop, "Stream stop."); \
return NULL; \
} \
ret = PyLong_FromLong((long) x); \
return ret; \
}
DATA(req)
DATA(res)
#define DATA_CONSUMED(TYPE) \
static PyObject *htpy_connp_##TYPE##_data_consumed(PyObject *self, PyObject *args) { \
PyObject *ret; \
ret = Py_BuildValue("I", htp_connp_##TYPE##_data_consumed(((htpy_connp *) self)->connp)); \
return ret; \
}
DATA_CONSUMED(req)
DATA_CONSUMED(res)
static PyObject *htpy_connp_get_last_error(PyObject *self, PyObject *args) {
htp_log_t *err = NULL;
err = htp_connp_get_last_error(((htpy_connp *) self)->connp);
if (!err)
Py_RETURN_NONE;
return Py_BuildValue("{sisssssi}", "level", err->level, "msg", err->msg, "file", err->file, "line", err->line);
}
static PyObject *htpy_connp_clear_error(PyObject *self, PyObject *args) {
htp_connp_clear_error(((htpy_connp *) self)->connp);
Py_RETURN_NONE;
}
static PyObject *htpy_connp_get_request_protocol(PyObject *self, PyObject *args) {
if (!((htpy_connp *) self)->connp->in_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->in_tx->request_protocol)
Py_RETURN_NONE;
return Py_BuildValue("s#", bstr_ptr(((htpy_connp *) self)->connp->in_tx->request_protocol), bstr_len(((htpy_connp *) self)->connp->in_tx->request_protocol));
}
static PyObject *htpy_connp_get_request_protocol_number(PyObject *self, PyObject *args) {
if (!((htpy_connp *) self)->connp->in_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->in_tx->request_protocol_number)
Py_RETURN_NONE;
return Py_BuildValue("i", ((htpy_connp *) self)->connp->in_tx->request_protocol_number);
}
static PyObject *htpy_connp_get_response_protocol(PyObject *self, PyObject *args) {
if (!((htpy_connp *) self)->connp->out_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->out_tx->response_protocol)
Py_RETURN_NONE;
return Py_BuildValue("s#", bstr_ptr(((htpy_connp *) self)->connp->out_tx->response_protocol), bstr_len(((htpy_connp *) self)->connp->out_tx->response_protocol));
}
static PyObject *htpy_connp_get_response_protocol_number(PyObject *self, PyObject *args) {
if (!((htpy_connp *) self)->connp->out_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->out_tx->response_protocol_number)
Py_RETURN_NONE;
return Py_BuildValue("i", ((htpy_connp *) self)->connp->out_tx->response_protocol_number);
}
static PyObject *htpy_connp_get_uri(PyObject *self, PyObject *args) {
htp_uri_t *uri;
int fail = 0;
PyObject *key, *val;
PyObject *ret = PyDict_New();
if (!ret) {
PyErr_SetString(htpy_error, "Unable to create new dictionary.");
return NULL;
}
/* Empty tx? That's odd. */
if (!((htpy_connp *) self)->connp->in_tx)
Py_RETURN_NONE;
if (!((htpy_connp *) self)->connp->in_tx->parsed_uri)
Py_RETURN_NONE;
uri = ((htpy_connp *) self)->connp->in_tx->parsed_uri;
if (uri->scheme) {
key = Py_BuildValue("s", "scheme");
val = Py_BuildValue("s", bstr_util_strdup_to_c(uri->scheme));
if (!key || !val)
fail = 1;
if (PyDict_SetItem(ret, key, val) == -1)
fail = 1;
Py_XDECREF(key);
Py_XDECREF(val);
}
if (uri->username) {
key = Py_BuildValue("s", "username");
val = Py_BuildValue("s", bstr_util_strdup_to_c(uri->username));
if (!key || !val)
fail = 1;
if (PyDict_SetItem(ret, key, val) == -1)
fail = 1;
Py_XDECREF(key);
Py_XDECREF(val);
}
if (uri->password) {
key = Py_BuildValue("s", "password");
val = Py_BuildValue("s", bstr_util_strdup_to_c(uri->password));
if (!key || !val)
fail = 1;
if (PyDict_SetItem(ret, key, val) == -1)
fail = 1;
Py_XDECREF(key);
Py_XDECREF(val);
}
if (uri->hostname) {
key = Py_BuildValue("s", "hostname");
val = Py_BuildValue("s", bstr_util_strdup_to_c(uri->hostname));
if (!key || !val)
fail = 1;
if (PyDict_SetItem(ret, key, val) == -1)
fail = 1;
Py_XDECREF(key);
Py_XDECREF(val);
}
if (uri->port) {
key = Py_BuildValue("s", "port");
val = Py_BuildValue("s", bstr_util_strdup_to_c(uri->port));
if (!key || !val)
fail = 1;
if (PyDict_SetItem(ret, key, val) == -1)
fail = 1;
Py_XDECREF(key);
Py_XDECREF(val);
}
if (uri->port_number) {
key = Py_BuildValue("s", "port_number");
val = Py_BuildValue("i", uri->port_number);
if (!key || !val)
fail = 1;
if (PyDict_SetItem(ret, key, val) == -1)
fail = 1;
Py_XDECREF(key);
Py_XDECREF(val);
}
if (uri->path) {
key = Py_BuildValue("s", "path");
val = Py_BuildValue("s", bstr_util_strdup_to_c(uri->path));
if (!key || !val)
fail = 1;
if (PyDict_SetItem(ret, key, val) == -1)
fail = 1;
Py_XDECREF(key);
Py_XDECREF(val);
}
if (uri->query) {
key = Py_BuildValue("s", "query");
val = Py_BuildValue("s", bstr_util_strdup_to_c(uri->query));
if (!key || !val)
fail = 1;
if (PyDict_SetItem(ret, key, val) == -1)
fail = 1;
Py_XDECREF(key);
Py_XDECREF(val);
}
if (uri->fragment) {
key = Py_BuildValue("s", "fragment");
val = Py_BuildValue("s", bstr_util_strdup_to_c(uri->fragment));
if (!key || !val)
fail = 1;
if (PyDict_SetItem(ret, key, val) == -1)
fail = 1;
Py_XDECREF(key);
Py_XDECREF(val);
}
// Exception should be set by Py_BuildValue or PyDict_SetItem failing.
if (fail) {
Py_DECREF(ret);
return NULL;
}
return ret;
}