-
Notifications
You must be signed in to change notification settings - Fork 7
/
event.c
1727 lines (1436 loc) · 45.6 KB
/
event.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) 2009, Kargo Global Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of the Kargo Global Inc. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Kargo Global Inc. ''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 Kargo Global Inc. 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.
*
* Author: Florian Hoenig <[email protected]>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_event.h"
#ifndef HAVE_TAILQFOREACH
#include <sys/queue.h>
#endif
/* include libevent header */
#include <event.h>
#include <evhttp.h>
/* network byteorder stuff */
#include <arpa/inet.h>
/* If you declare any globals in php_event.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(event)
*/
/* True global resources - no need for thread safety here */
static int le_evhttp;
static int le_evhttp_request;
static int le_evbuffer;
static int le_event;
static int le_bufferevent;
static int le_evhttp_connection;
static int le_evhttp_response;
static struct event_base *current_base;
/* Forward declarations */
static void event_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
static void bufferevent_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
static void evhttp_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
static void evhttp_request_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
static void evhttp_connection_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
static void evhttp_response_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
/* Function argument info decarations */
#ifdef ZEND_ENGINE_2
ZEND_BEGIN_ARG_INFO(bufferevent_read_byref_arginfo, 0)
ZEND_ARG_PASS_INFO(0)
ZEND_ARG_PASS_INFO(1)
ZEND_ARG_PASS_INFO(0)
ZEND_END_ARG_INFO()
#else
static unsigned char bufferevent_read_byref_arginfo[] = { 2, BYREF_FORCE, BYREF_NONE };
#endif
/* {{{ event_functions[]
*
* Every user visible function must have an entry in event_functions[].
*/
zend_function_entry event_functions[] = {
PHP_FE(event_init, NULL)
PHP_FE(event_reinit, NULL)
PHP_FE(event_new, NULL)
PHP_FE(event_free, NULL)
PHP_FE(event_set, NULL)
PHP_FE(event_add, NULL)
PHP_FE(event_del, NULL)
PHP_FE(event_dispatch, NULL)
PHP_FE(event_loopbreak, NULL)
PHP_FE(evhttp_start, NULL)
PHP_FE(evhttp_connection_new, NULL)
PHP_FE(evhttp_connection_set_closecb, NULL)
PHP_FE(evhttp_request_new, NULL)
PHP_FE(evhttp_request_free, NULL)
PHP_FE(evhttp_make_request, NULL)
PHP_FE(evhttp_set_gencb, NULL)
PHP_FE(evhttp_request_get_uri, NULL)
PHP_FE(evhttp_request_method, NULL)
PHP_FE(evhttp_request_body, NULL)
PHP_FE(evhttp_request_append_body, NULL)
PHP_FE(evhttp_request_input_buffer, NULL)
PHP_FE(evhttp_request_find_header, NULL)
PHP_FE(evhttp_request_headers, NULL)
PHP_FE(evhttp_request_add_header, NULL)
PHP_FE(evhttp_request_status, NULL)
PHP_FE(evhttp_response_set, NULL)
PHP_FE(evhttp_response_add_header, NULL)
PHP_FE(evbuffer_new, NULL)
PHP_FE(evbuffer_free, NULL)
PHP_FE(evbuffer_add, NULL)
PHP_FE(evbuffer_readline, NULL)
PHP_FE(bufferevent_new, NULL)
PHP_FE(bufferevent_enable, NULL)
PHP_FE(bufferevent_disable, NULL)
PHP_FE(bufferevent_read, bufferevent_read_byref_arginfo)
PHP_FE(bufferevent_write, NULL)
PHP_FE(ntohs, NULL)
PHP_FE(ntohl, NULL)
PHP_FE(htons, NULL)
PHP_FE(htonl, NULL)
{NULL, NULL, NULL} /* Must be the last line in event_functions[] */
};
/* }}} */
/* {{{ event_module_entry
*/
zend_module_entry event_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"event",
event_functions,
PHP_MINIT(event),
PHP_MSHUTDOWN(event),
NULL, /* Replace with NULL if there's nothing to do at request start */
NULL, /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(event),
#if ZEND_MODULE_API_NO >= 20010901
"0.2", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_EVENT
ZEND_GET_MODULE(event)
#endif
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("event.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_event_globals, event_globals)
STD_PHP_INI_ENTRY("event.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_event_globals, event_globals)
PHP_INI_END()
*/
/* }}} */
/* {{{ php_event_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_event_init_globals(zend_event_globals *event_globals)
{
event_globals->global_value = 0;
event_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(event)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
/* resource types */
le_evhttp = zend_register_list_destructors_ex(evhttp_dtor, NULL, PHP_EVHTTP_RES_NAME, module_number);
le_evhttp_request = zend_register_list_destructors_ex(evhttp_request_dtor, NULL, PHP_EVHTTP_REQUEST_RES_NAME, module_number);
le_evhttp_connection = zend_register_list_destructors_ex(evhttp_connection_dtor, NULL, PHP_EVHTTP_CONNECTION_RES_NAME, module_number);
le_evbuffer = zend_register_list_destructors_ex(NULL, NULL, PHP_EVBUFFER_RES_NAME, module_number);
le_event = zend_register_list_destructors_ex(event_dtor, NULL, PHP_EVENT_RES_NAME, module_number);
le_bufferevent = zend_register_list_destructors_ex(bufferevent_dtor, NULL, PHP_BUFFEREVENT_RES_NAME, module_number);
le_evhttp_response = zend_register_list_destructors_ex(evhttp_response_dtor, NULL, PHP_EVHTTP_RESPONSE_RES_NAME, module_number);
/* libevent constants */
REGISTER_LONG_CONSTANT("EV_READ", EV_READ, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EV_WRITE", EV_WRITE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EV_TIMEOUT", EV_TIMEOUT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EV_SIGNAL", EV_SIGNAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EV_PERSIST", EV_PERSIST, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EVBUFFER_EOF", EVBUFFER_EOF, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EVBUFFER_ERROR", EVBUFFER_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EVHTTP_REQ_GET", EVHTTP_REQ_GET, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EVHTTP_REQ_POST", EVHTTP_REQ_POST, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EVHTTP_REQ_HEAD", EVHTTP_REQ_HEAD, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EVHTTP_REQUEST", EVHTTP_REQUEST, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EVHTTP_RESPONSE", EVHTTP_RESPONSE, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(event)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(event)
{
php_info_print_table_start();
php_info_print_table_header(2, "event support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
PHP_FUNCTION(event_init)
{
current_base = event_init();
}
PHP_FUNCTION(event_reinit)
{
event_reinit(current_base);
}
PHP_FUNCTION(event_dispatch)
{
event_dispatch();
}
PHP_FUNCTION(event_loopbreak)
{
if (event_loopbreak() == 0)
{
RETURN_TRUE;
}
else
{
RETURN_FALSE;
}
}
/*
* Create an event struct and register it as resource
*/
PHP_FUNCTION(event_new)
{
struct event *e;
if (!(e = malloc(sizeof(struct event))))
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not create event resource");
RETURN_FALSE;
}
e->ev_arg = NULL;
//php_printf("Event created...\n");
ZEND_REGISTER_RESOURCE(return_value, e, le_event);
}
/*
* Destructor for bufferevent resource
*/
static void bufferevent_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
struct bufferevent *e = (struct bufferevent*)rsrc->ptr;
int i;
if (e->cbarg != NULL)
{
zval_dtor(((php_bufferevent*)(e->cbarg))->r_cb);
FREE_ZVAL(((php_bufferevent*)(e->cbarg))->r_cb);
zval_dtor(((php_bufferevent*)(e->cbarg))->w_cb);
FREE_ZVAL(((php_bufferevent*)(e->cbarg))->w_cb);
zval_dtor(((php_bufferevent*)(e->cbarg))->e_cb);
FREE_ZVAL(((php_bufferevent*)(e->cbarg))->e_cb);
zval_dtor(((php_bufferevent*)(e->cbarg))->stream);
zend_list_delete( Z_RESVAL_P(((php_event*)(e->cbarg))->stream) );
FREE_ZVAL(((php_bufferevent*)(e->cbarg))->stream);
FREE_ZVAL(((php_bufferevent*)(e->cbarg))->res_bufferevent);
free(e->cbarg);
}
bufferevent_free(e);
}
/*
* Destructor for evhttp resource
*/
static void evhttp_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
evhttp_free((struct evhttp*)rsrc->ptr);
}
/*
* Destructor for evhttp_request resource
*/
static void evhttp_request_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
}
/*
* Destructor for evhttp_connection resource
*/
static void evhttp_connection_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
evhttp_connection_free((struct evhttp_connection*)rsrc->ptr);
}
/*
* Destructor for our evhttp_response resource
*/
static void evhttp_response_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
evhttp_response *r = (evhttp_response*)rsrc->ptr;
free(r->res_message);
if (r->res_body_len > 0)
{
free(r->res_body);
}
free(r);
}
/*
* Destructor for event resource
*/
static void event_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
struct event *e = (struct event*)rsrc->ptr;
int i;
if (event_initialized(e) == EVLIST_INIT)
{
if (e->ev_arg != NULL)
{
zval_dtor(((php_event*)(e->ev_arg))->cb);
FREE_ZVAL(((php_event*)(e->ev_arg))->cb);
FREE_ZVAL(((php_event*)(e->ev_arg))->flags);
if (IS_RESOURCE == Z_TYPE_P(((php_event*)(e->ev_arg))->stream))
zend_list_delete( Z_RESVAL_P(((php_event*)(e->ev_arg))->stream) );
free(e->ev_arg);
}
event_del(e);
}
free(e);
}
/*
* Generic event callback to call into user land
*/
void php_event_callback_handler(int fd, short ev, void *arg)
{
php_event *event = (php_event*)arg;
#ifdef ZTS
TSRMLS_D = event->TSRMLS_C;
#endif
zval **params[3];
zval *retval = NULL;
//zval *z_stream;
//MAKE_STD_ZVAL(z_stream);
//ZVAL_RESOURCE(z_stream, php_stream_get_resource_id(event->stream));
//zend_list_addref(php_stream_get_resource_id(event->stream));
//php_debug_zval_dump(&(event->event));
params[0] = &(event->stream);
params[1] = &(event->flags);
params[2] = &(event->event);
/* callback into php */
if (call_user_function_ex(EG(function_table), NULL, event->cb, &retval, 3, params, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Callback failed");
}
if (retval)
zval_ptr_dtor(&retval);
//zval_ptr_dtor(params[0]);
//zval_ptr_dtor(params[2]);
//zval_ptr_dtor(&z_stream);
//zend_list_delete(php_stream_get_resource_id(event->stream));
return;
}
PHP_FUNCTION(event_set)
{
struct event *e;
char *callable;
long event_flags;
int fd;
zval *e_res, *z_cb, *z_stream;
php_stream *stream;
php_event *event_args;
/* parse parameters */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzlz", &e_res, &z_stream, &event_flags, &z_cb) == FAILURE)
{
RETURN_FALSE;
}
/* fetch event struct */
e = (struct event*) zend_fetch_resource(&e_res TSRMLS_CC, -1, PHP_EVENT_RES_NAME, NULL, 1, le_event);
if (e == NULL)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "First argument must be a valid event resource");
RETURN_FALSE;
}
/* check signal vs stream */
if (IS_LONG == Z_TYPE_P(z_stream))
{
// we got a signal event. z_stream contains signal number
fd = Z_LVAL_P(z_stream);
}
else
{
/* fetch stream */
php_stream_from_zval(stream, &z_stream);
if (!stream)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid stream");
RETURN_FALSE;
}
if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fd, 1) || fd == -1)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Incompatible stream");
RETURN_FALSE;
}
/* set non-blocking and non-buffered to not let it step on our hands here */
php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 0, NULL);
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
}
#ifdef ZEND_ENGINE_2
if (!zend_make_callable(z_cb, &callable TSRMLS_CC))
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid callback: '%s'", callable);
return;
}
#endif
efree(callable);
/* setup event */
event_args = (php_event*)malloc(sizeof(php_event));
event_args->cb = z_cb;
Z_ADDREF_P(event_args->cb);
MAKE_STD_ZVAL(event_args->flags);
ZVAL_LONG(event_args->flags, event_flags);
event_args->stream = z_stream;
event_args->event = e_res;
event_set(e, fd, event_flags, php_event_callback_handler, (void*)event_args);
RETURN_TRUE;
}
PHP_FUNCTION(event_add)
{
zval *e_res, *z_sec, *z_usec;
struct event *e;
struct timeval timeout;
int ret;
int argc;
argc = ZEND_NUM_ARGS();
/* parse parameters */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|zz", &e_res, &z_sec, &z_usec) == FAILURE)
{
RETURN_FALSE;
}
/* fetch event struct */
e = (struct event*) zend_fetch_resource(&e_res TSRMLS_CC, -1, PHP_EVENT_RES_NAME, NULL, 1, le_event);
if (e == NULL)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "First argument must be a valid event resource");
RETURN_FALSE;
}
/* add event to scheduler */
timeout.tv_sec = (argc > 1 && Z_TYPE_P(z_sec) == IS_LONG) ? Z_LVAL_P(z_sec) : 0;
timeout.tv_usec = (argc > 2 && Z_TYPE_P(z_usec) == IS_LONG) ? Z_LVAL_P(z_usec) : 0;
if (argc > 1)
ret = event_add(e, &timeout);
else
ret = event_add(e, 0);
/* check if it worked */
if (ret == 0)
{
RETURN_TRUE;
}
else
{
RETURN_FALSE;
}
}
PHP_FUNCTION(event_del)
{
zval *e_res;
struct event *e;
/* parse parameters */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &e_res) == FAILURE)
{
RETURN_FALSE;
}
/* fetch event struct */
e = (struct event*) zend_fetch_resource(&e_res TSRMLS_CC, -1, PHP_EVENT_RES_NAME, NULL, 1, le_event);
if (e == NULL)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "First argument must be a valid event resource");
RETURN_FALSE;
}
/* remove event from scheduler */
if (event_del(e) == 0)
{
RETURN_TRUE;
}
else
{
RETURN_FALSE;
}
}
PHP_FUNCTION(event_free)
{
zval *e_res;
/* parse parameters */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &e_res) == FAILURE)
{
RETURN_FALSE;
}
zval_dtor(e_res);
}
PHP_FUNCTION(evhttp_response_set)
{
long http_code;
char *content, *http_message;
int content_len, http_message_len;
evhttp_response *response;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &content, &content_len, &http_code, &http_message, &http_message_len) == FAILURE)
{
RETURN_FALSE;
}
/* TODO: use evbuffer here, instead of passing string around in the struct */
response = malloc(sizeof(evhttp_response));
response->res_code = http_code;
response->res_message = strdup(http_message);
response->res_body_len = content_len;
if (content_len > 0)
{
response->res_body = malloc(content_len+1);
memcpy(response->res_body, content, content_len);
}
ZEND_REGISTER_RESOURCE(return_value, response, le_evhttp_response);
}
void php_callback_handler(struct evhttp_request *req, void *arg)
{
zval *cb;
zval *retval = NULL;
zval **params[1];
zval *req_resource;
cb = ((evhttp_callback_arg *)arg)->arg;
int res;
struct evbuffer *buf;
evhttp_response *response;
char *str;
int str_len;
int res_id;
void *retval_res;
int retval_res_type;
#ifdef ZTS
TSRMLS_D = ((evhttp_callback_arg *) arg)->TSRMLS_C;
#endif
/* pass the request as a php resource */
MAKE_STD_ZVAL(req_resource);
res_id = zend_register_resource(req_resource, req, le_evhttp_request);
params[0] = &req_resource;
res = call_user_function_ex(EG(function_table), NULL, cb, &retval, 1, params, 0, NULL TSRMLS_CC);
/* free resource */
req->cb_arg = NULL;
zend_list_delete(res_id);
FREE_ZVAL(req_resource);
if (!retval)
{
/* We got nothing back from user callback */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Request callback returned nothing");
buf = evbuffer_new();
evhttp_send_reply(req, HTTP_SERVUNAVAIL, "ERR", buf);
return;
}
switch (retval->type)
{
case IS_RESOURCE:
/* the callback is allowed to return either a simple evbuffer, or
* a custom response (incl. response code/message) */
retval_res = zend_list_find(Z_RESVAL_P(retval), &retval_res_type);
if (retval_res_type == le_evbuffer)
{
buf = (struct evbuffer*) retval_res;
/* got a evbuffer back. send it! */
evhttp_send_reply(req, HTTP_OK, "OK", buf);
} else if (retval_res_type == le_evhttp_response) {
/* got a response resource back */
response = (evhttp_response *) retval_res;
buf = evbuffer_new();
if (response->res_body_len > 0)
{
evbuffer_add(buf, response->res_body, response->res_body_len);
}
evhttp_send_reply(req, response->res_code, response->res_message, buf);
} else {
/* We got the wrong resource type */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Request callback returned illegal resource");
buf = evbuffer_new();
evhttp_send_reply(req, HTTP_SERVUNAVAIL, "ERR", buf);
}
break;
case IS_STRING:
/* return value was a string */
str = Z_STRVAL_P(retval);
str_len = Z_STRLEN_P(retval);
buf = evbuffer_new();
evbuffer_add(buf, str, str_len);
evhttp_send_reply(req, HTTP_OK, "OK", buf);
break;
default:
/* We got nothing back from user callback */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Request callback returned wrong datatype");
buf = evbuffer_new();
evhttp_send_reply(req, HTTP_OK, "OK", buf);
}
zval_ptr_dtor(&retval);
evbuffer_free(buf);
return;
}
PHP_FUNCTION(evhttp_start)
{
struct evhttp *httpd;
char *listen_ip;
long port;
int listen_ip_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &listen_ip, &listen_ip_len, &port) == FAILURE)
{
RETURN_NULL();
}
httpd = evhttp_start(listen_ip, port);
if (!httpd)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Error binding httpd on %s port %d", listen_ip, port);
RETURN_FALSE;
}
/* Set timeout to a reasonably short value for performance [BB-MH] */
evhttp_set_timeout(httpd, 10);
ZEND_REGISTER_RESOURCE(return_value, httpd, le_evhttp);
}
PHP_FUNCTION(evhttp_set_gencb)
{
struct evhttp *httpd;
zval *res_httpd, *php_cb;
zval *cb;
char *callable = NULL;
evhttp_callback_arg *cb_arg = (evhttp_callback_arg *) emalloc(sizeof(evhttp_callback_arg));
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &res_httpd, &php_cb) == FAILURE)
{
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(httpd, struct evhttp*, &res_httpd, -1, PHP_EVHTTP_RES_NAME, le_evhttp);
#ifdef ZEND_ENGINE_2
if (!zend_make_callable(php_cb, &callable TSRMLS_CC))
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "function '%s' is not a valid callback", callable);
efree(callable);
return;
}
#endif
MAKE_STD_ZVAL(cb_arg->arg);
*(cb_arg->arg) = *php_cb;
zval_copy_ctor(cb_arg->arg);
#ifdef ZTS
cb_arg->TSRMLS_C = TSRMLS_C;
#endif
evhttp_set_gencb(httpd, php_callback_handler, (void *)cb_arg);
}
PHP_FUNCTION(evhttp_request_get_uri)
{
struct evhttp_request *req;
zval *res_req;
zval *z_uri;
const char *uri;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res_req) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch resource");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(req, struct evhttp_request*, &res_req, -1, PHP_EVHTTP_REQUEST_RES_NAME, le_evhttp_request);
uri = evhttp_request_get_uri(req);
ZVAL_STRING(return_value, (char*)uri, 1);
return;
}
PHP_FUNCTION(evhttp_request_method)
{
struct evhttp_request *req;
zval *res_req;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res_req) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch resource");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(req, struct evhttp_request*, &res_req, -1, PHP_EVHTTP_REQUEST_RES_NAME, le_evhttp_request);
/* replicate logic of libevent's internal method (evhttp_method), which isn't exposed in the library */
switch (req->type) {
case EVHTTP_REQ_GET:
RETURN_STRING("GET", 1);
case EVHTTP_REQ_POST:
RETURN_STRING("POST", 1);
case EVHTTP_REQ_HEAD:
RETURN_STRING("HEAD", 1);
case EVHTTP_REQ_PUT:
RETURN_STRING("PUT", 1);
case EVHTTP_REQ_DELETE:
RETURN_STRING("DELETE", 1);
case EVHTTP_REQ_OPTIONS:
RETURN_STRING("OPTIONS", 1);
case EVHTTP_REQ_TRACE:
RETURN_STRING("TRACE", 1);
case EVHTTP_REQ_CONNECT:
RETURN_STRING("CONNECT", 1);
default:
RETURN_NULL();
}
}
PHP_FUNCTION(evhttp_request_find_header)
{
struct evhttp_request *req;
zval *res_req;
const char *headervalue;
char *headername;
int headername_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &res_req, &headername, &headername_len) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch resource");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(req, struct evhttp_request*, &res_req, -1, PHP_EVHTTP_REQUEST_RES_NAME, le_evhttp_request);
if ((headervalue = evhttp_find_header(req->input_headers, headername)) == NULL)
{
/* header not found */
RETURN_NULL();
}
ZVAL_STRING(return_value, (char*)headervalue, 1);
return;
}
PHP_FUNCTION(evhttp_request_headers)
{
struct evhttp_request *req;
zval *res_req;
struct evkeyval *header;
struct evkeyvalq *q;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res_req) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch resource");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(req, struct evhttp_request*, &res_req, -1, PHP_EVHTTP_REQUEST_RES_NAME, le_evhttp_request);
array_init(return_value);
q = req->input_headers;
TAILQ_FOREACH (header, q, next)
{
add_assoc_string(return_value, header->key, header->value, TRUE);
}
return;
}
PHP_FUNCTION(evhttp_request_add_header)
{
struct evhttp_request *req;
zval *res_req;
char *headername, *headervalue;
int headername_len, headervalue_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &res_req, &headername, &headername_len, &headervalue, &headervalue_len) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error reading parameters");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(req, struct evhttp_request*, &res_req, -1, PHP_EVHTTP_REQUEST_RES_NAME, le_evhttp_request);
if (evhttp_add_header(req->input_headers, headername, headervalue) != 0)
{
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(evhttp_response_add_header)
{
struct evhttp_request *req;
zval *res_req;
char *headername, *headervalue;
int headername_len, headervalue_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &res_req, &headername, &headername_len, &headervalue, &headervalue_len) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error reading parameters");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(req, struct evhttp_request*, &res_req, -1, PHP_EVHTTP_REQUEST_RES_NAME, le_evhttp_request);
// NOTE: only difference of response headers is below
if (evhttp_add_header(req->output_headers, headername, headervalue) != 0)
{
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(evhttp_request_status)
{
struct evhttp_request *req;
zval *res_req;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res_req) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error reading parameters");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(req, struct evhttp_request*, &res_req, -1, PHP_EVHTTP_REQUEST_RES_NAME, le_evhttp_request);
RETURN_LONG(req->response_code);
}
PHP_FUNCTION(evhttp_request_input_buffer)
{
struct evhttp_request *req;
zval *res_req;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res_req) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch resource");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(req, struct evhttp_request*, &res_req, -1, PHP_EVHTTP_REQUEST_RES_NAME, le_evhttp_request);
ZEND_REGISTER_RESOURCE(return_value, req->input_buffer, le_evbuffer);
}
PHP_FUNCTION(evhttp_request_body)
{
struct evhttp_request *req;
zval *res_req;
int body_len, status;
char *body;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res_req) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch resource");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(req, struct evhttp_request*, &res_req, -1, PHP_EVHTTP_REQUEST_RES_NAME, le_evhttp_request);
body_len = EVBUFFER_LENGTH(req->input_buffer);
if (req->input_buffer == NULL || body_len == 0)
{
RETURN_FALSE;
}
body = emalloc(body_len + 1);
status = evbuffer_remove(req->input_buffer, (void *) body, body_len);
body[body_len] = '\0';
if (status == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not get data from resource");
RETURN_FALSE;
}
ZVAL_STRING(return_value, body, 0);
}
PHP_FUNCTION(evhttp_request_append_body)
{
struct evhttp_request *req;
zval *res_req;
int body_len;
char *body;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &res_req, &body, &body_len) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error reading params");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(req, struct evhttp_request*, &res_req, -1, PHP_EVHTTP_REQUEST_RES_NAME, le_evhttp_request);
if (req->output_buffer == NULL)
{
RETURN_FALSE;
}
if (evbuffer_add(req->output_buffer, body, body_len) != 0)
{
RETURN_FALSE;
}
RETURN_TRUE;