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
47 typedef struct _php_event_callback_t
{ /* {{{ */
50 } php_event_callback_t
;
53 php_event_callback_t
*callback
= NULL
;
55 static int setnonblock(int fd
)
59 flags
= fcntl(fd
, F_GETFL
);
63 if (fcntl(fd
, F_SETFL
, flags
) < 0)
69 static inline void _php_event_callback_free(php_event_callback_t
*this_callback
) /* {{{ */
75 zval_ptr_dtor(&this_callback
->func
);
76 if (this_callback
->arg
) {
77 zval_ptr_dtor(&this_callback
->arg
);
82 static void timeout_cb(struct ev_loop
*loop
, struct ev_timer
*w
, int revents
) {
83 struct client
*cli
= (struct client
*) w
->data
;
84 assert(w
== &cli
->ev_timeout
);
86 ev_timer_start(loop
, &cli
->ev_goodbye
);
89 static void goodbye_cb(struct ev_loop
*loop
, struct ev_timer
*w
, int revents
) {
90 struct client
*cli
= (struct client
*) w
->data
;
91 assert(w
== &cli
->ev_goodbye
);
93 ev_io_stop(loop
, &cli
->ev_read
);
94 ev_io_stop(loop
, &cli
->ev_write
);
95 ev_timer_stop(loop
, &cli
->ev_timeout
);
100 static void write_cb(struct ev_loop
*loop
, struct ev_io
*w
, int revents
)
102 struct client
*cli
= (struct client
*) w
->data
;
104 if(EV_ERROR
& revents
) {
105 ev_timer_start(loop
, &cli
->ev_goodbye
);
109 if (revents
& EV_WRITE
) {
110 if (Z_TYPE_P(&cli
->retval
) == IS_STRING
) {
111 char *test
= (char *) malloc((256 + Z_STRLEN_P(&cli
->retval
)) * sizeof(char));
112 int i
= snprintf(test
, 1024, "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",
113 (timeout
== 0.0 ? "close" : "keep-alive"), Z_STRLEN_P(&cli
->retval
), Z_STRVAL_P(&cli
->retval
));
114 write(cli
->fd
, test
, i
);
121 zval_dtor(&cli
->retval
);
123 if (timeout
== 0.0) {
124 ev_timer_start(loop
, &cli
->ev_goodbye
);
126 ev_timer_again(loop
, &cli
->ev_timeout
);
127 ev_io_start(loop
, &cli
->ev_read
);
131 static void read_cb(struct ev_loop
*loop
, struct ev_io
*w
, int revents
)
133 if ((revents
& EV_READ
) && callback
) {
134 char *rbuff
= emalloc(1024);
136 struct client
*cli
= (struct client
*) w
->data
;
138 r
= read(cli
->fd
,rbuff
,1023);
141 zval
*http_headers
[1];
145 MAKE_STD_ZVAL(http_headers
[0]);
146 ZVAL_STRINGL (http_headers
[0], rbuff
, r
, 0);
148 if (call_user_function(EG(function_table
), NULL
, callback
->func
, &cli
->retval
, 1, http_headers TSRMLS_CC
) == SUCCESS
) {
149 ev_io_start(loop
,&cli
->ev_write
);
150 zval_dtor(http_headers
[0]); /* Free's the string */
151 FREE_ZVAL(http_headers
[0]); /* Free's the object itself */
152 return; /* NO PROCESSING ANYMORE!! */
154 zval_dtor(http_headers
[0]);
155 FREE_ZVAL(http_headers
[0]);
156 zval_dtor(&cli
->retval
);
163 struct client
*cli
= (struct client
*) w
->data
;
165 ev_timer_start(loop
, &cli
->ev_goodbye
);
168 static void accept_cb(struct ev_loop
*loop
, struct ev_io
*w
, int revents
)
172 struct sockaddr_in client_addr
;
173 socklen_t client_len
= sizeof(client_addr
);
174 client_fd
= accept(w
->fd
, (struct sockaddr
*)&client_addr
, &client_len
);
178 cli
= calloc(1,sizeof(struct client
));
180 if (setnonblock(cli
->fd
) < 0)
181 err(1, "failed to set client socket to non-blocking");
183 ev_io_init(&cli
->ev_read
, read_cb
, cli
->fd
, EV_READ
);
184 cli
->ev_read
.data
= cli
;
186 ev_io_init(&cli
->ev_write
, write_cb
, cli
->fd
, EV_WRITE
);
187 cli
->ev_write
.data
= cli
;
189 ev_timer_init(&cli
->ev_goodbye
, goodbye_cb
, 0., 0.);
190 cli
->ev_goodbye
.data
= cli
;
192 ev_timer_init(&cli
->ev_timeout
, timeout_cb
, 0., (timeout
== 0.0 ? 30.0 : timeout
));
193 cli
->ev_timeout
.data
= cli
;
195 ev_io_start(loop
,&cli
->ev_read
);
198 /* {{{ mistral_functions[] */
199 zend_function_entry mistral_functions
[] = {
200 PHP_FE(mistral_init
, NULL
)
201 PHP_FE(mistral_register_callback
, NULL
)
202 PHP_FE(mistral_start
, NULL
)
207 /* {{{ mistral_module_entry
209 zend_module_entry mistral_module_entry
= {
210 STANDARD_MODULE_HEADER
,
213 NULL
, /* Replace with NULL if there is nothing to do at php startup */
214 PHP_MSHUTDOWN(mistral
),
215 NULL
, /* Replace with NULL if there is nothing to do at request start */
216 NULL
, /* Replace with NULL if there is nothing to do at request end */
219 STANDARD_MODULE_PROPERTIES
223 #ifdef COMPILE_DL_MISTRAL
224 ZEND_GET_MODULE(mistral
)
227 /* {{{ PHP_MINFO_FUNCTION */
228 PHP_MINFO_FUNCTION(mistral
)
230 php_info_print_table_start();
231 php_info_print_table_header(2, "Mistral support", "enabled");
232 php_info_print_table_row(2, "Version", PHP_MISTRAL_VERSION
);
233 php_info_print_table_end();
237 /* {{{ PHP_MSHUTDOWN_FUNCTION */
238 PHP_MSHUTDOWN_FUNCTION (mistral
)
240 _php_event_callback_free(callback
);
244 PHP_FUNCTION(mistral_init
)
246 struct addrinfo
*ai
= NULL
;
247 struct addrinfo hints
;
248 struct addrinfo
*runp
;
249 int listen_sock
= -1;
255 struct sockaddr_storage sin
;
256 struct sockaddr_in
* sa4
= (struct sockaddr_in
*) &sin
;
257 struct sockaddr_in6
* sa6
= (struct sockaddr_in6
*) &sin
;
259 int reuseaddr_on
= 1;
260 int keepalive_on
= 1;
262 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sld", &listen_addr
, &listen_addr_len
, &listen_port
, &timeout
) == FAILURE
) {
266 if (listen_port
< 1 || listen_port
> 65535) {
270 bzero(&hints
, sizeof (hints
));
271 hints
.ai_flags
= AI_ADDRCONFIG
|| AI_NUMERICSERV
;
272 hints
.ai_socktype
= SOCK_STREAM
;
274 if (getaddrinfo (listen_addr
, "", &hints
, &ai
) != 0) {
280 while (runp
!= NULL
&& listen_sock
< 0) {
281 listen_sock
= socket(runp
->ai_family
, SOCK_STREAM
, 0);
283 runp
= runp
->ai_next
;
286 if (listen_sock
< 0) {
287 err(1, "listen failed");
290 if (timeout
<= 0.0) {
293 if (setsockopt(listen_sock
, SOL_SOCKET
, SO_KEEPALIVE
, &keepalive_on
, sizeof(keepalive_on
)) == -1)
294 err(1, "setsockopt failed");
297 if (setsockopt(listen_sock
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr_on
, sizeof(reuseaddr_on
)) == -1)
298 err(1, "setsockopt failed");
300 if (runp
->ai_family
== AF_INET6
) {
302 setsockopt(listen_sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &null
, sizeof(null
));
305 memset(&sin
, 0, sizeof(sin
));
307 sin
.ss_family
= runp
->ai_family
;
308 switch (sin
.ss_family
) {
310 inet_pton(sin
.ss_family
, listen_addr
, &(sa6
->sin6_addr
));
311 sa6
->sin6_port
= htons(listen_port
);
314 if ( strchr(listen_addr
, ':') )
315 /* Invalid IPv4-string. Use the wildcard address. */
316 listen_addr
= strdup("0.0.0.0");
318 inet_pton(sin
.ss_family
, listen_addr
, &(sa4
->sin_addr
));
319 sa4
->sin_port
= htons(listen_port
);
325 if (bind(listen_sock
, (struct sockaddr
*) &sin
, sizeof(sin
)) < 0)
326 err(1, "bind failed");
328 if (listen(listen_sock
, 5) < 0)
329 err(1, "listen failed");
331 if (setnonblock(listen_sock
) < 0)
332 err(1, "failed to set server socket to non-blocking");
334 ev_io_init(&ev_accept
, accept_cb
, listen_sock
, EV_READ
);
337 /* {{{ proto bool event_set(resource event, resource fd, int events, mixed callback[, mixed arg])
338 * Taken from pecl::libevent event_set
340 PHP_FUNCTION(mistral_register_callback
)
342 zval
*zcallback
, *zarg
= NULL
;
343 php_event_callback_t
*new_callback
, *old_callback
;
346 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zcallback
) != SUCCESS
) {
350 if (!zend_is_callable(zcallback
, 0, &func_name TSRMLS_CC
)) {
351 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "'%s' is not a valid callback", func_name
);
357 zval_add_ref(&zcallback
);
359 new_callback
= emalloc(sizeof(php_event_callback_t
));
360 new_callback
->func
= zcallback
;
361 new_callback
->arg
= zarg
;
363 old_callback
= callback
;
364 callback
= new_callback
;
367 _php_event_callback_free(old_callback
);
373 PHP_FUNCTION(mistral_start
)
375 struct ev_loop
*loop
= ev_default_loop (0);
377 ev_io_start(loop
, &ev_accept
);