6 #include "ext/standard/info.h"
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
29 * We did use some ideas from:
30 * https://sie.isc.org/svn/nmsg-httpk/trunk/nmsg-httpk.c
31 * http://github.com/ry/libebb/blob/master/ebb.c
52 typedef struct _php_event_callback_t
{ /* {{{ */
55 } php_event_callback_t
;
58 php_event_callback_t
*callback
= NULL
;
60 static int setnonblock(int fd
)
64 flags
= fcntl(fd
, F_GETFL
);
68 if (fcntl(fd
, F_SETFL
, flags
) < 0)
74 static inline void _php_event_callback_free(php_event_callback_t
*this_callback
) /* {{{ */
80 zval_ptr_dtor(&this_callback
->func
);
81 if (this_callback
->arg
) {
82 zval_ptr_dtor(&this_callback
->arg
);
87 static void timeout_cb(struct ev_loop
*loop
, struct ev_timer
*w
, int revents
) {
88 struct client
*cli
= (struct client
*) w
->data
;
89 assert(w
== &cli
->ev_timeout
);
91 ev_timer_start(loop
, &cli
->ev_goodbye
);
94 static void goodbye_cb(struct ev_loop
*loop
, struct ev_timer
*w
, int revents
) {
95 struct client
*cli
= (struct client
*) w
->data
;
96 assert(w
== &cli
->ev_goodbye
);
98 ev_io_stop(loop
, &cli
->ev_read
);
99 ev_io_stop(loop
, &cli
->ev_write
);
100 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 /* Gracefully stolen concept from ebb_pthread */
131 void* execute_php(void *_cli
) {
133 struct client
*cli
= (struct client
*) _cli
;
136 zval
*http_headers
[1];
138 cli
->rbuff
[cli
->rlen
] = '\0';
140 MAKE_STD_ZVAL(http_headers
[0]);
141 ZVAL_STRINGL (http_headers
[0], cli
->rbuff
, (cli
->rlen
+ 1), 0);
143 result
= call_user_function(EG(function_table
), NULL
, callback
->func
, &retval
, 1, http_headers TSRMLS_CC
);
147 FREE_ZVAL(http_headers
[0]); /* Free's the object itself */
150 if (result
== SUCCESS
) {
151 if (Z_TYPE_P(&retval
) == IS_STRING
) {
152 cli
->rlen
= 256 + Z_STRLEN_P(&retval
);
153 cli
->rbuff
= (char *) malloc(cli
->rlen
* sizeof(char));
154 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",
155 (timeout
== 0.0 ? "close" : "keep-alive"), Z_STRLEN_P(&retval
), Z_STRVAL_P(&retval
));
157 } else if (Z_TYPE_P(&retval
) == IS_ARRAY
) {
158 HashTable
*arr_hash
= Z_ARRVAL_P(&retval
);
159 HashPosition pointer
;
161 int header_len
= 11; /* HTTP/1.1 \r\n */
164 char *status_code
= NULL
;
168 for (zend_hash_internal_pointer_reset_ex(arr_hash
, &pointer
);
169 zend_hash_get_current_data_ex(arr_hash
, (void**) &data
, &pointer
) == SUCCESS
;
170 zend_hash_move_forward_ex(arr_hash
, &pointer
)) {
175 if (zend_hash_get_current_key_ex(arr_hash
, &key
, &key_len
, &index
, 0, &pointer
) == HASH_KEY_IS_STRING
) {
177 zval_copy_ctor(&temp
);
178 convert_to_string(&temp
);
180 if (Z_STRLEN(temp
) > 0) {
181 if (php_strcmp("status_code", key
, key_len
- 1)) {
182 status_code
= strdup(Z_STRVAL(temp
));
183 status_len
+= Z_STRLEN(temp
);
184 } else if (php_strcmp("body", key
, key_len
- 1)) {
185 body_len
+= Z_STRLEN(temp
);
186 } else { /* So it is a header */
187 header_len
+= key_len
- 1;
188 header_len
+= 4; /* : \r\n */
189 header_len
+= Z_STRLEN(temp
);
191 if (php_strcasecmp("Connection", key
, key_len
- 1) && php_strcasecmp("close", Z_STRVAL(temp
), Z_STRLEN(temp
))) {
200 cli
->rlen
= (header_len
+ (status_len
== 0 ? 8 : status_len
+ 2) + body_len
);
201 cli
->rbuff
= (char *) malloc(cli
->rlen
* sizeof(char));
204 if (status_len
== 0) {
205 strncpy(test
, "HTTP/1.1 200 OK\r\n", 17);
208 strncpy(test
, "HTTP/1.1 ", 9);
210 strncpy(test
, status_code
, status_len
);
212 strncpy(test
, "\r\n", 2);
217 for (zend_hash_internal_pointer_reset_ex(arr_hash
, &pointer
);
218 zend_hash_get_current_data_ex(arr_hash
, (void**) &data
, &pointer
) == SUCCESS
;
219 zend_hash_move_forward_ex(arr_hash
, &pointer
)) {
224 if (zend_hash_get_current_key_ex(arr_hash
, &key
, &key_len
, &index
, 0, &pointer
) == HASH_KEY_IS_STRING
) {
226 zval_copy_ctor(&temp
);
227 convert_to_string(&temp
);
229 if (Z_STRLEN(temp
) > 0) {
230 if (status_len
> 0 && php_strcmp("status_code", key
, key_len
- 1)) {
232 } else if (body_len
> 0 && php_strcmp("body", key
, key_len
- 1)) {
233 body
= strdup(Z_STRVAL(temp
));
234 } else { /* So it is a header */
235 strncpy(test
, key
, key_len
- 1);
237 strncpy(test
, ": ", 2);
239 strncpy(test
, Z_STRVAL(temp
), Z_STRLEN(temp
));
240 test
+= Z_STRLEN(temp
);
241 strncpy(test
, "\r\n", 2);
248 strncpy(test
, "\r\n", 2);
252 strncpy(test
, body
, body_len
);
258 ev_io_start(cli
->loop
,&cli
->ev_write
);
263 if (result
!= SUCCESS
) {
264 ev_timer_start(cli
->loop
, &cli
->ev_goodbye
);
268 static void read_cb(struct ev_loop
*loop
, struct ev_io
*w
, int revents
)
270 if ((revents
& EV_READ
) && callback
) {
271 struct client
*cli
= (struct client
*) w
->data
;
272 cli
->rbuff
= malloc(1024);
274 cli
->rlen
= read(cli
->fd
,cli
->rbuff
,1023);
279 pthread_create(&thread
, NULL
, execute_php
, cli
);
280 pthread_detach(thread
);
290 struct client
*cli
= (struct client
*) w
->data
;
292 ev_timer_start(loop
, &cli
->ev_goodbye
);
295 static void accept_cb(struct ev_loop
*loop
, struct ev_io
*w
, int revents
)
299 struct sockaddr_in client_addr
;
300 socklen_t client_len
= sizeof(client_addr
);
301 client_fd
= accept(w
->fd
, (struct sockaddr
*)&client_addr
, &client_len
);
305 cli
= calloc(1,sizeof(struct client
));
309 if (setnonblock(cli
->fd
) < 0)
310 err(1, "failed to set client socket to non-blocking");
312 ev_io_init(&cli
->ev_read
, read_cb
, cli
->fd
, EV_READ
);
313 cli
->ev_read
.data
= cli
;
315 ev_io_init(&cli
->ev_write
, write_cb
, cli
->fd
, EV_WRITE
);
316 cli
->ev_write
.data
= cli
;
318 ev_timer_init(&cli
->ev_goodbye
, goodbye_cb
, 0., 0.);
319 cli
->ev_goodbye
.data
= cli
;
321 ev_timer_init(&cli
->ev_timeout
, timeout_cb
, 0., (timeout
== 0.0 ? 30.0 : timeout
));
322 cli
->ev_timeout
.data
= cli
;
324 ev_io_start(loop
,&cli
->ev_read
);
327 /* {{{ mistral_functions[] */
328 zend_function_entry mistral_functions
[] = {
329 PHP_FE(mistral_init
, NULL
)
330 PHP_FE(mistral_register_callback
, NULL
)
331 PHP_FE(mistral_start
, NULL
)
336 /* {{{ mistral_module_entry
338 zend_module_entry mistral_module_entry
= {
339 STANDARD_MODULE_HEADER
,
342 NULL
, /* Replace with NULL if there is nothing to do at php startup */
343 PHP_MSHUTDOWN(mistral
),
344 NULL
, /* Replace with NULL if there is nothing to do at request start */
345 NULL
, /* Replace with NULL if there is nothing to do at request end */
348 STANDARD_MODULE_PROPERTIES
352 #ifdef COMPILE_DL_MISTRAL
353 ZEND_GET_MODULE(mistral
)
356 /* {{{ PHP_MINFO_FUNCTION */
357 PHP_MINFO_FUNCTION(mistral
)
359 php_info_print_table_start();
360 php_info_print_table_header(2, "Mistral support", "enabled");
361 php_info_print_table_row(2, "Version", PHP_MISTRAL_VERSION
);
362 php_info_print_table_end();
366 /* {{{ PHP_MSHUTDOWN_FUNCTION */
367 PHP_MSHUTDOWN_FUNCTION (mistral
)
369 _php_event_callback_free(callback
);
373 PHP_FUNCTION(mistral_init
)
375 struct addrinfo
*ai
= NULL
;
376 struct addrinfo hints
;
377 struct addrinfo
*runp
;
378 int listen_sock
= -1;
384 struct sockaddr_storage sin
;
385 struct sockaddr_in
* sa4
= (struct sockaddr_in
*) &sin
;
386 struct sockaddr_in6
* sa6
= (struct sockaddr_in6
*) &sin
;
388 int reuseaddr_on
= 1;
389 int keepalive_on
= 1;
391 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sld", &listen_addr
, &listen_addr_len
, &listen_port
, &timeout
) == FAILURE
) {
395 if (listen_port
< 1 || listen_port
> 65535) {
399 bzero(&hints
, sizeof (hints
));
400 hints
.ai_flags
= AI_ADDRCONFIG
|| AI_NUMERICSERV
;
401 hints
.ai_socktype
= SOCK_STREAM
;
403 if (getaddrinfo (listen_addr
, "", &hints
, &ai
) != 0) {
409 while (runp
!= NULL
&& listen_sock
< 0) {
410 listen_sock
= socket(runp
->ai_family
, SOCK_STREAM
, 0);
412 runp
= runp
->ai_next
;
415 if (listen_sock
< 0) {
416 err(1, "listen failed");
419 if (timeout
<= 0.0) {
422 if (setsockopt(listen_sock
, SOL_SOCKET
, SO_KEEPALIVE
, &keepalive_on
, sizeof(keepalive_on
)) == -1)
423 err(1, "setsockopt failed");
426 if (setsockopt(listen_sock
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr_on
, sizeof(reuseaddr_on
)) == -1)
427 err(1, "setsockopt failed");
429 if (runp
->ai_family
== AF_INET6
) {
431 setsockopt(listen_sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &null
, sizeof(null
));
434 memset(&sin
, 0, sizeof(sin
));
436 sin
.ss_family
= runp
->ai_family
;
437 switch (sin
.ss_family
) {
439 inet_pton(sin
.ss_family
, listen_addr
, &(sa6
->sin6_addr
));
440 sa6
->sin6_port
= htons(listen_port
);
443 if ( strchr(listen_addr
, ':') )
444 /* Invalid IPv4-string. Use the wildcard address. */
445 listen_addr
= strdup("0.0.0.0");
447 inet_pton(sin
.ss_family
, listen_addr
, &(sa4
->sin_addr
));
448 sa4
->sin_port
= htons(listen_port
);
454 if (bind(listen_sock
, (struct sockaddr
*) &sin
, sizeof(sin
)) < 0)
455 err(1, "bind failed");
457 if (listen(listen_sock
, 5) < 0)
458 err(1, "listen failed");
460 if (setnonblock(listen_sock
) < 0)
461 err(1, "failed to set server socket to non-blocking");
463 ev_io_init(&ev_accept
, accept_cb
, listen_sock
, EV_READ
);
466 /* {{{ proto bool event_set(resource event, resource fd, int events, mixed callback[, mixed arg])
467 * Taken from pecl::libevent event_set
469 PHP_FUNCTION(mistral_register_callback
)
472 php_event_callback_t
*new_callback
, *old_callback
;
476 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zcallback
) != SUCCESS
) {
480 if (!zend_is_callable(zcallback
, 0, &func_name TSRMLS_CC
)) {
481 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "'%s' is not a valid callback", func_name
);
491 new_callback
= emalloc(sizeof(php_event_callback_t
));
492 new_callback
->func
= cb
;
493 new_callback
->arg
= NULL
;
495 old_callback
= callback
;
496 callback
= new_callback
;
499 _php_event_callback_free(old_callback
);
505 PHP_FUNCTION(mistral_start
)
507 struct ev_loop
*loop
= ev_default_loop (0);
509 ev_io_start(loop
, &ev_accept
);