Commits

Ruslan Osmanov committed 1f3db82

Fix: within EventHttp::accept() make fd nonblocking
Fix: memory leak in EventHttp's callback
Add: some HTTP samples

Comments (0)

Files changed (5)

 	PHP_EVENT_FETCH_HTTP_REQ(http_req, arg_req);
 	http_req->ptr      = req;
 	http_req->internal = 1; /* Don't evhttp_request_free(req) */
-	Z_ADDREF_P(arg_req);
 	args[0] = &arg_req;
 
 	if (arg_data) {
 	if (fd < 0) {
 		RETURN_FALSE;
 	}
+	evutil_make_socket_nonblocking(fd);
 
 	PHP_EVENT_FETCH_HTTP(http, zhttp);
 

examples/http_accept.php

+<?php
+$base = new EventBase();
+$http = new EventHttp($base);
+
+$addresses = array (
+ 	8091 => "127.0.0.1",
+ 	8092 => "127.0.0.2",
+);
+$i = 0;
+
+$socket = array();
+
+foreach ($addresses as $port => $ip) {
+	echo $ip, " ", $port, PHP_EOL;
+	$socket[$i] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+	if (!socket_bind($socket[$i], $ip, $port)) {
+		exit("socket_bind failed\n");
+	}
+	socket_listen($socket[$i], 0);
+	socket_set_nonblock($socket[$i]);
+
+	if (!$http->accept($socket[$i])) {
+		echo "Accept failed\n";
+		exit(1);
+	}
+
+	++$i;
+}
+
+$http->setDefaultCallback(function($req) {
+	echo "URI: ", $req->getUri(), PHP_EOL;
+	$req->sendReply(200, "OK");
+	echo "OK\n";
+});
+
+$signal = Event::signal($base, SIGINT, function () use ($base) {
+	echo "Caught SIGINT. Stopping...\n";
+	$base->stop();
+});
+$signal->add();
+
+$base->dispatch();
+echo "END\n";
+// We didn't close sockets, since Libevent already sets CLOSE_ON_FREE and CLOSE_ON_EXEC flags on the file 
+// descriptor associated with the sockets.
+?>

examples/http_bind.php

+<?php
+
+$base = new EventBase();
+$http = new EventHttp($base);
+
+$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+
+if (!$http->bind("127.0.0.1", 8088)) {
+	exit("bind(1) failed\n");
+};
+if (!$http->bind("127.0.0.1", 8089)) {
+	exit("bind(2) failed\n");
+
+};
+
+$http->setCallback("/about", function($req) {
+	echo "URI: ", $req->getUri(), PHP_EOL;
+	$req->sendReply(200, "OK");
+	echo "OK\n";
+});
+$base->dispatch();
+
+?>

examples/http_request.php

 $conn->makeRequest($req, EventHttpRequest::CMD_GET, "/index.cphp");
 
 $base->loop();
-$base;
 ?>
 	PHP_EVENT_FREE_FCALL_INFO(http->fci, http->fcc);
 
 	/* Free attached callbacks */
-	PHP_EVENT_ASSERT(http->cb_head);
+	/*PHP_EVENT_ASSERT(http->cb_head);*/
 	cb = http->cb_head;
 	while (cb) {
 		cb_next = cb->next;