6 #include "ext/standard/info.h"
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
28 * We did use some ideas from:
29 * https://sie.isc.org/svn/nmsg-httpk/trunk/nmsg-httpk.c
30 * http://github.com/ry/libebb/blob/master/ebb.c
51 typedef struct _php_event_callback_t
{ /* {{{ */
54 } php_event_callback_t
;
57 php_event_callback_t
*callback
= NULL
;
59 static int setnonblock(int fd
)
63 flags
= fcntl(fd
, F_GETFL
);
67 if (fcntl(fd
, F_SETFL
, flags
) < 0)
73 static inline void _php_event_callback_free(php_event_callback_t
*this_callback
) /* {{{ */
79 zval_ptr_dtor(&this_callback
->func
);
80 if (this_callback
->arg
) {
81 zval_ptr_dtor(&this_callback
->arg
);
86 static void timeout_cb(struct ev_loop
*loop
, struct ev_timer
*w
, int revents
) {
87 struct client
*cli
= (struct client
*) w
->data
;
88 assert(w
== &cli
->ev_timeout
);
90 ev_timer_start(loop
, &cli
->ev_goodbye
);
93 static void goodbye_cb(struct ev_loop
*loop
, struct ev_timer
*w
, int revents
) {
94 struct client
*cli
= (struct client
*) w
->data
;
95 assert(w
== &cli
->ev_goodbye
);
97 ev_io_stop(loop
, &cli
->ev_read
);
98 ev_io_stop(loop
, &cli
->ev_write
);
99 ev_timer_stop(loop
, &cli
->ev_timeout
);
105 static void write_cb(struct ev_loop
*loop
, struct ev_io
*w
, int revents
)
107 struct client
*cli
= (struct client
*) w
->data
;
109 if (EV_ERROR
& revents
) {
110 ev_timer_start(loop
, &cli
->ev_goodbye
);
114 if (revents
& EV_WRITE
) {
116 write(cli
->fd
, cli
->rbuff
, cli
->rlen
);
122 if (cli
->close
== 1 || timeout
== 0.0) {
123 ev_timer_start(loop
, &cli
->ev_goodbye
);
125 ev_timer_again(loop
, &cli
->ev_timeout
);
126 ev_io_start(loop
, &cli
->ev_read
);
130 static inline void* execute_php(void *_cli
) {
132 struct client
*cli
= (struct client
*) _cli
;
135 zval
*http_headers
[1];
137 cli
->rbuff
[cli
->rlen
] = '\0';
139 MAKE_STD_ZVAL(http_headers
[0]);
140 ZVAL_STRINGL (http_headers
[0], cli
->rbuff
, (cli
->rlen
+ 1), 0);
142 result
= call_user_function(EG(function_table
), NULL
, callback
->func
, &retval
, 1, http_headers TSRMLS_CC
);
146 FREE_ZVAL(http_headers
[0]); /* Free's the object itself */
148 if (result
== SUCCESS
) {
149 if (Z_TYPE_P(&retval
) == IS_STRING
) {
150 cli
->rlen
= 256 + Z_STRLEN_P(&retval
);
151 cli
->rbuff
= (char *) malloc(cli
->rlen
* sizeof(char));
152 cli
->rlen
= snprintf(cli
->rbuff
, (cli
->rlen
- 1), "HTTP/1.1 200 OK\r\nConnection: %s\r\nContent-Type: text/plain\r\nServer: mistral/0.1\r\nContent-Length: %u\r\n\r\n%s",
153 (timeout
== 0.0 ? "close" : "keep-alive"), Z_STRLEN_P(&retval
), Z_STRVAL_P(&retval
));
155 } else if (Z_TYPE_P(&retval
) == IS_ARRAY
) {
156 HashTable
*arr_hash
= Z_ARRVAL_P(&retval
);
157 HashPosition pointer
;
159 int header_len
= 11; /* HTTP/1.1 \r\n */
162 char *status_code
= NULL
;
166 for (zend_hash_internal_pointer_reset_ex(arr_hash
, &pointer
);
167 zend_hash_get_current_data_ex(arr_hash
, (void**) &data
, &pointer
) == SUCCESS
;
168 zend_hash_move_forward_ex(arr_hash
, &pointer
)) {
173 if (zend_hash_get_current_key_ex(arr_hash
, &key
, &key_len
, &index
, 0, &pointer
) == HASH_KEY_IS_STRING
) {
175 zval_copy_ctor(&temp
);
176 convert_to_string(&temp
);
178 if (Z_STRLEN(temp
) > 0) {
179 if (php_strcmp("status_code", key
, key_len
- 1)) {
180 status_code
= strdup(Z_STRVAL(temp
));
181 status_len
+= Z_STRLEN(temp
);
182 } else if (php_strcmp("body", key
, key_len
- 1)) {
183 body_len
+= Z_STRLEN(temp
);
184 } else { /* So it is a header */
185 header_len
+= key_len
- 1;
186 header_len
+= 4; /* : \r\n */
187 header_len
+= Z_STRLEN(temp
);
189 if (php_strcasecmp("Connection", key
, key_len
- 1) && php_strcasecmp("close", Z_STRVAL(temp
), Z_STRLEN(temp
))) {
198 cli
->rlen
= (header_len
+ (status_len
== 0 ? 8 : status_len
+ 2) + body_len
);
199 cli
->rbuff
= (char *) malloc(cli
->rlen
* sizeof(char));
202 if (status_len
== 0) {
203 strncpy(test
, "HTTP/1.1 200 OK\r\n", 17);
206 strncpy(test
, "HTTP/1.1 ", 9);
208 strncpy(test
, status_code
, status_len
);
210 strncpy(test
, "\r\n", 2);
215 for (zend_hash_internal_pointer_reset_ex(arr_hash
, &pointer
);
216 zend_hash_get_current_data_ex(arr_hash
, (void**) &data
, &pointer
) == SUCCESS
;
217 zend_hash_move_forward_ex(arr_hash
, &pointer
)) {
222 if (zend_hash_get_current_key_ex(arr_hash
, &key
, &key_len
, &index
, 0, &pointer
) == HASH_KEY_IS_STRING
) {
224 zval_copy_ctor(&temp
);
225 convert_to_string(&temp
);
227 if (Z_STRLEN(temp
) > 0) {
228 if (status_len
> 0 && php_strcmp("status_code", key
, key_len
- 1)) {
230 } else if (body_len
> 0 && php_strcmp("body", key
, key_len
- 1)) {
231 body
= strdup(Z_STRVAL(temp
));
232 } else { /* So it is a header */
233 strncpy(test
, key
, key_len
- 1);
235 strncpy(test
, ": ", 2);
237 strncpy(test
, Z_STRVAL(temp
), Z_STRLEN(temp
));
238 test
+= Z_STRLEN(temp
);
239 strncpy(test
, "\r\n", 2);
247 strncpy(test
, "\r\n", 2);
251 strncpy(test
, body
, body_len
);
257 ev_io_start(cli
->loop
,&cli
->ev_write
);
262 if (result
!= SUCCESS
) {
263 ev_timer_start(cli
->loop
, &cli
->ev_goodbye
);
267 static void read_cb(struct ev_loop
*loop
, struct ev_io
*w
, int revents
)
269 if ((revents
& EV_READ
) && callback
) {
270 struct client
*cli
= (struct client
*) w
->data
;
271 cli
->rbuff
= malloc(1024);
273 cli
->rlen
= read(cli
->fd
,cli
->rbuff
,1023);
287 struct client
*cli
= (struct client
*) w
->data
;
289 ev_timer_start(loop
, &cli
->ev_goodbye
);
292 static void accept_cb(struct ev_loop
*loop
, struct ev_io
*w
, int revents
)
296 struct sockaddr_in client_addr
;
297 socklen_t client_len
= sizeof(client_addr
);
298 client_fd
= accept(w
->fd
, (struct sockaddr
*)&client_addr
, &client_len
);
302 cli
= calloc(1,sizeof(struct client
));
306 if (setnonblock(cli
->fd
) < 0)
307 err(1, "failed to set client socket to non-blocking");
309 ev_io_init(&cli
->ev_read
, read_cb
, cli
->fd
, EV_READ
);
310 cli
->ev_read
.data
= cli
;
312 ev_io_init(&cli
->ev_write
, write_cb
, cli
->fd
, EV_WRITE
);
313 cli
->ev_write
.data
= cli
;
315 ev_timer_init(&cli
->ev_goodbye
, goodbye_cb
, 0., 0.);
316 cli
->ev_goodbye
.data
= cli
;
318 ev_timer_init(&cli
->ev_timeout
, timeout_cb
, 0., (timeout
== 0.0 ? 30.0 : timeout
));
319 cli
->ev_timeout
.data
= cli
;
321 ev_io_start(loop
,&cli
->ev_read
);
324 /* {{{ mistral_functions[] */
325 zend_function_entry mistral_functions
[] = {
326 PHP_FE(mistral_init
, NULL
)
327 PHP_FE(mistral_register_callback
, NULL
)
328 PHP_FE(mistral_start
, NULL
)
333 /* {{{ mistral_module_entry
335 zend_module_entry mistral_module_entry
= {
336 STANDARD_MODULE_HEADER
,
339 NULL
, /* Replace with NULL if there is nothing to do at php startup */
340 PHP_MSHUTDOWN(mistral
),
341 NULL
, /* Replace with NULL if there is nothing to do at request start */
342 NULL
, /* Replace with NULL if there is nothing to do at request end */
345 STANDARD_MODULE_PROPERTIES
349 #ifdef COMPILE_DL_MISTRAL
350 ZEND_GET_MODULE(mistral
)
353 /* {{{ PHP_MINFO_FUNCTION */
354 PHP_MINFO_FUNCTION(mistral
)
356 php_info_print_table_start();
357 php_info_print_table_header(2, "Mistral support", "enabled");
358 php_info_print_table_row(2, "Version", PHP_MISTRAL_VERSION
);
359 php_info_print_table_end();
363 /* {{{ PHP_MSHUTDOWN_FUNCTION */
364 PHP_MSHUTDOWN_FUNCTION (mistral
)
366 _php_event_callback_free(callback
);
370 PHP_FUNCTION(mistral_init
)
372 struct addrinfo
*ai
= NULL
;
373 struct addrinfo hints
;
374 struct addrinfo
*runp
;
375 int listen_sock
= -1;
381 struct sockaddr_storage sin
;
382 struct sockaddr_in
* sa4
= (struct sockaddr_in
*) &sin
;
383 struct sockaddr_in6
* sa6
= (struct sockaddr_in6
*) &sin
;
385 int reuseaddr_on
= 1;
386 int keepalive_on
= 1;
388 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sld", &listen_addr
, &listen_addr_len
, &listen_port
, &timeout
) == FAILURE
) {
392 if (listen_port
< 1 || listen_port
> 65535) {
396 bzero(&hints
, sizeof (hints
));
397 hints
.ai_flags
= AI_ADDRCONFIG
|| AI_NUMERICSERV
;
398 hints
.ai_socktype
= SOCK_STREAM
;
400 if (getaddrinfo(listen_addr
, "", &hints
, &ai
) != 0) {
406 while (runp
!= NULL
&& listen_sock
< 0) {
407 listen_sock
= socket(runp
->ai_family
, SOCK_STREAM
, 0);
409 runp
= runp
->ai_next
;
412 if (listen_sock
< 0) {
413 err(1, "listen failed");
416 if (timeout
<= 0.0) {
419 if (setsockopt(listen_sock
, SOL_SOCKET
, SO_KEEPALIVE
, &keepalive_on
, sizeof(keepalive_on
)) == -1)
420 err(1, "setsockopt failed");
423 if (setsockopt(listen_sock
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr_on
, sizeof(reuseaddr_on
)) == -1)
424 err(1, "setsockopt failed");
426 if (runp
->ai_family
== AF_INET6
) {
428 setsockopt(listen_sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &null
, sizeof(null
));
431 memset(&sin
, 0, sizeof(sin
));
433 sin
.ss_family
= runp
->ai_family
;
434 switch (sin
.ss_family
) {
436 inet_pton(sin
.ss_family
, listen_addr
, &(sa6
->sin6_addr
));
437 sa6
->sin6_port
= htons(listen_port
);
440 if ( strchr(listen_addr
, ':') )
441 /* Invalid IPv4-string. Use the wildcard address. */
442 listen_addr
= strdup("0.0.0.0");
444 inet_pton(sin
.ss_family
, listen_addr
, &(sa4
->sin_addr
));
445 sa4
->sin_port
= htons(listen_port
);
451 if (bind(listen_sock
, (struct sockaddr
*) &sin
, sizeof(sin
)) < 0)
452 err(1, "bind failed");
454 if (listen(listen_sock
, 5) < 0)
455 err(1, "listen failed");
457 if (setnonblock(listen_sock
) < 0)
458 err(1, "failed to set server socket to non-blocking");
460 ev_io_init(&ev_accept
, accept_cb
, listen_sock
, EV_READ
);
463 /* {{{ proto bool event_set(resource event, resource fd, int events, mixed callback[, mixed arg])
464 * Taken from pecl::libevent event_set
466 PHP_FUNCTION(mistral_register_callback
)
468 zval
*zcallback
, *zarg
= NULL
;
469 php_event_callback_t
*new_callback
, *old_callback
;
472 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zcallback
) != SUCCESS
) {
476 if (!zend_is_callable(zcallback
, 0, &func_name TSRMLS_CC
)) {
477 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "'%s' is not a valid callback", func_name
);
483 zval_add_ref(&zcallback
);
485 new_callback
= emalloc(sizeof(php_event_callback_t
));
486 new_callback
->func
= zcallback
;
487 new_callback
->arg
= zarg
;
489 old_callback
= callback
;
490 callback
= new_callback
;
493 _php_event_callback_free(old_callback
);
500 PHP_FUNCTION(mistral_start
)
502 struct ev_loop
*loop
= ev_default_loop(0);
504 ev_io_start(loop
, &ev_accept
);